redis.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package cache
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/go-redis/redis/v8"
  11. jsoniter "github.com/json-iterator/go"
  12. )
  13. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  14. type Cache struct {
  15. client *redis.Client
  16. prefix string
  17. }
  18. type ChannelUserCache struct {
  19. ID int64
  20. Name string
  21. Permission []string `json:"permission"`
  22. IsTemp bool `json:"isTemp"`
  23. ValidArchivesIds []string `json:"validArchivesIds"`
  24. }
  25. type ManagerCache struct {
  26. ID int64
  27. }
  28. type MemberCache struct {
  29. ID int64
  30. Name string
  31. }
  32. var cache = &Cache{}
  33. func Instance() *Cache {
  34. return cache
  35. }
  36. func GetClient() *redis.Client {
  37. return cache.client
  38. }
  39. func InitRedis() *redis.Client {
  40. RedisDB, err := strconv.Atoi(os.Getenv("REDIS_DB"))
  41. if err != nil {
  42. log.Panic("Redis数据库错误", err)
  43. }
  44. // 初始化Cache
  45. return InitRedisClient(fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT")), os.Getenv("REDIS_PASSWORD"), "lumen_cache:", RedisDB)
  46. }
  47. func InitRedisClient(addr, password, p string, db int) *redis.Client {
  48. client := redis.NewClient(&redis.Options{
  49. Addr: addr,
  50. Password: password,
  51. DB: db,
  52. })
  53. if err := client.Ping(context.Background()).Err(); err != nil {
  54. log.Panic("start cache", err)
  55. }
  56. cache.prefix = p
  57. cache.client = client
  58. return client
  59. }
  60. func (c *Cache) KeyWithPrefix(key string) string {
  61. return c.prefix + key
  62. }
  63. func (c *Cache) Get(key string) (string, error) {
  64. value, err := c.client.Get(context.Background(), c.KeyWithPrefix(key)).Result()
  65. if err != nil {
  66. return "", err
  67. }
  68. // PHP序列化之后的对象无法直接转为go的数据结构,直接都当数组处理
  69. value = strings.ReplaceAll(value, `O:8:"stdClass"`, `a`)
  70. value = strings.ReplaceAll(value, `O:17:"App\Models\Member"`, `a`)
  71. value = strings.ReplaceAll(value, `O:15:"App\Models\User"`, `a`)
  72. return value, nil
  73. }
  74. func (c *Cache) Put(key string, value interface{}, expiration time.Duration) error {
  75. valueStr, err := json.MarshalToString(value)
  76. if err != nil {
  77. return err
  78. }
  79. ok, err := c.client.SetEX(context.Background(), c.KeyWithPrefix(key), valueStr, expiration).Result()
  80. fmt.Println(ok, err)
  81. return err
  82. }
  83. func (c *Cache) PutStrForever(key string, value string) error {
  84. _, err := c.client.Set(context.Background(), c.KeyWithPrefix(key), value, 0).Result()
  85. return err
  86. }
  87. func (c *Cache) PutStr(key string, value string, expiration time.Duration) error {
  88. ok, err := c.client.SetEX(context.Background(), c.KeyWithPrefix(key), value, expiration).Result()
  89. fmt.Println(ok, err)
  90. return err
  91. }
  92. func (c *Cache) Delete(key string) (int64, error) {
  93. return c.client.Del(context.Background(), c.KeyWithPrefix(key)).Result()
  94. }
  95. func (c *Cache) Incr(key string) (int64, error) {
  96. return c.client.Incr(context.Background(), c.KeyWithPrefix(key)).Result()
  97. }