cache_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package cache
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "testing"
  6. "time"
  7. cherryTime "github.com/cherry-game/cherry/extend/time"
  8. "github.com/goburrow/cache"
  9. )
  10. func TestMaximumSize(t *testing.T) {
  11. fmt.Println("TestMaximumSize")
  12. testCache := cache.New(
  13. cache.WithMaximumSize(3),
  14. cache.WithExpireAfterAccess(3*time.Second),
  15. )
  16. for i := 1; i <= 4; i++ {
  17. testCache.Put(i, i)
  18. }
  19. time.Sleep(3 * time.Second)
  20. v, found := testCache.GetIfPresent(1)
  21. t.Log(v, found)
  22. }
  23. func TestKeyInt64(t *testing.T) {
  24. var key, value int64
  25. key = 247749712289988672
  26. value = 247749712289988672
  27. fmt.Println(key, value)
  28. testCache := cache.New(
  29. cache.WithMaximumSize(5),
  30. cache.WithExpireAfterAccess(5*time.Second), //访问后的5秒钟过期
  31. )
  32. testCache.Put(key, value)
  33. v, found := testCache.GetIfPresent(key)
  34. fmt.Println(v, found)
  35. }
  36. func Test111(t *testing.T) {
  37. testCache := cache.New(
  38. cache.WithMaximumSize(5),
  39. cache.WithExpireAfterAccess(5*time.Second),
  40. )
  41. for i := 1; i <= 5; i++ {
  42. testCache.Put(i, i)
  43. }
  44. for i := 1; i <= 35; i++ {
  45. val, found := testCache.GetIfPresent(1)
  46. fmt.Printf("key = %d, val = %+v, found = %v\n", i, val, found)
  47. time.Sleep(1 * time.Second)
  48. }
  49. }
  50. func TestCache(t *testing.T) {
  51. load := func(k cache.Key) (cache.Value, error) {
  52. time.Sleep(100 * time.Millisecond) // Slow task
  53. return fmt.Sprintf("%d", k), nil
  54. }
  55. // CreateGroup a loading cache
  56. c := cache.NewLoadingCache(load,
  57. cache.WithMaximumSize(100), // Limit number of entries in the cache.
  58. cache.WithExpireAfterAccess(3*time.Second), // Expire entries after 1 minute since last accessed.
  59. cache.WithRefreshAfterWrite(3*time.Second), // Expire entries after 2 minutes since last created.
  60. )
  61. getTicker := time.Tick(4 * time.Second)
  62. reportTicker := time.Tick(5 * time.Second)
  63. for {
  64. select {
  65. case <-getTicker:
  66. _, _ = c.Get(rand.Intn(200))
  67. case <-reportTicker:
  68. st := cache.Stats{}
  69. c.Stats(&st)
  70. fmt.Printf("%+v\n", st)
  71. }
  72. }
  73. }
  74. func TestRefreshAfterWrite(t *testing.T) {
  75. testCache := cache.NewLoadingCache(
  76. refreshAfterWrite,
  77. cache.WithMaximumSize(32),
  78. cache.WithExpireAfterWrite(4*time.Second),
  79. //cache.WithRefreshAfterWrite(4*time.Second),
  80. )
  81. for {
  82. val, _ := testCache.Get("a")
  83. fmt.Println(val)
  84. time.Sleep(2 * time.Second)
  85. }
  86. }
  87. func refreshAfterWrite(key cache.Key) (cache.Value, error) {
  88. fmt.Println("refresh " + cherryTime.Now().String())
  89. return rand.Intn(500), nil
  90. }