z_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package cache
  2. import (
  3. "log"
  4. "testing"
  5. "time"
  6. )
  7. func TestMap(t *testing.T) {
  8. m := NewMap(time.Second*5, 10)
  9. m.Set("key", 10)
  10. log.Println(m.Get("key"))
  11. m.Delete("key")
  12. log.Println(m.Get("key"))
  13. log.Println(m.Get("key1"))
  14. go func() {
  15. for e := range m.CleanEvent() {
  16. log.Println("CLEANED", e)
  17. }
  18. }()
  19. for i := 0; i < 5; i++ {
  20. time.Sleep(time.Second * 10)
  21. m.Set("key", "oneee")
  22. }
  23. }
  24. func TestMapCheck(t *testing.T) {
  25. m := NewMap(time.Hour, 0)
  26. m.Set("key", 10)
  27. log.Println(m.GetCheck("key", func(key, value interface{}, exists bool) (rKey, rVal interface{}, needUpdate bool) {
  28. log.Println("MCHECK", key, value, exists)
  29. rVal = "Cool!!!"
  30. rKey, needUpdate = "key1", true
  31. return
  32. }))
  33. log.Println("===================")
  34. log.Println(m.Get("key"))
  35. log.Println(m.Get("key1"))
  36. log.Println(m.GetCreateNew("key", func(key interface{}) (rKey, val interface{}, created bool) {
  37. return "oooone", 1000, true
  38. }))
  39. }
  40. func TestMapEach(t *testing.T) {
  41. m := NewMap(time.Second*5, 10)
  42. m.SetMulti(map[interface{}]interface{}{
  43. 1: "one",
  44. 2: "two",
  45. 3: "three",
  46. })
  47. m.Each(func(key, val interface{}) bool {
  48. log.Println(key, val)
  49. return false
  50. })
  51. }
  52. func TestKeys(t *testing.T) {
  53. m := NewMap(time.Second*5, 20)
  54. m.SetMulti(map[interface{}]interface{}{
  55. "one": 1,
  56. "two": 2,
  57. })
  58. log.Println(m.Keys())
  59. m.Clear()
  60. log.Println(m.Keys())
  61. }