cache_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package test
  2. import (
  3. "context"
  4. "github.com/goflyfox/gtoken/gtoken"
  5. "github.com/gogf/gf/v2/encoding/gjson"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/os/gcache"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "testing"
  10. "time"
  11. )
  12. func TestGCache(t *testing.T) {
  13. t.Log("gcache test ")
  14. ctx := context.TODO()
  15. userKey := "123123"
  16. gcache.Set(ctx, userKey, "1", 10000)
  17. value, err := gcache.Get(ctx, userKey)
  18. if err != nil {
  19. t.Error("cache set error," + err.Error())
  20. }
  21. if value.String() == userKey {
  22. t.Error("cache get error")
  23. }
  24. gcache.Remove(ctx, userKey)
  25. value, err = gcache.Get(ctx, userKey)
  26. if err != nil {
  27. t.Error("cache set error," + err.Error())
  28. }
  29. if value != nil {
  30. t.Error("cache remove error")
  31. }
  32. }
  33. func TestRedisCache(t *testing.T) {
  34. ctx := context.TODO()
  35. gVar, err := g.Cfg().Get(ctx, "gToken.CacheMode")
  36. if err != nil {
  37. t.Error("error:", err)
  38. }
  39. if gVar.Int() != gtoken.CacheModeRedis {
  40. t.Log("redis cache not test ")
  41. return
  42. }
  43. t.Log("redis cache test ")
  44. userKey := "test:a"
  45. _, err = g.Redis().Do(ctx, "SETEX", userKey, 10000, "1")
  46. if err != nil {
  47. t.Error("cache set error," + err.Error())
  48. }
  49. time.Sleep(1 * time.Second)
  50. ttl, err2 := g.Redis().Do(ctx, "TTL", userKey)
  51. if err2 != nil {
  52. t.Error("cache ttl error," + err.Error())
  53. }
  54. t.Log("ttl:" + gconv.String(ttl))
  55. if gconv.Int(ttl) >= 10000 || gconv.Int(ttl) < 9000 {
  56. t.Error("cache ttl error, ttl:" + gconv.String(ttl))
  57. }
  58. data, err3 := g.Redis().Do(ctx, "GET", userKey)
  59. if err3 != nil {
  60. t.Error("cache get error," + err.Error())
  61. }
  62. t.Log("data:" + gconv.String(data))
  63. if gconv.String(data) != "1" {
  64. t.Error("cache get error, data:" + gconv.String(data))
  65. }
  66. g.Redis().Do(ctx, "DEL", userKey)
  67. data, err4 := g.Redis().Do(ctx, "GET", userKey)
  68. if err4 != nil {
  69. t.Error("cache del get error," + err.Error())
  70. }
  71. if gconv.String(data) != "" {
  72. t.Error("cache del error, data:" + gconv.String(data))
  73. }
  74. }
  75. func TestJson(t *testing.T) {
  76. t.Log("json test ")
  77. cacheValue := g.Map{
  78. "userKey": "123",
  79. "uuid": "abc",
  80. "data": "",
  81. }
  82. cacheValueJson, err1 := gjson.Encode(cacheValue)
  83. if err1 != nil {
  84. t.Error("cache json encode error:" + err1.Error())
  85. }
  86. var userCache g.Map
  87. err2 := gjson.DecodeTo(cacheValueJson, &userCache)
  88. if err2 != nil {
  89. t.Error("cache get json error:" + err2.Error())
  90. }
  91. if gconv.Map(userCache)["userKey"] != "123" {
  92. t.Error("cache get json data error:" + gconv.String(userCache))
  93. }
  94. }