Browse Source

添加 'cache/redis.go'

cuiguohai 2 days ago
parent
commit
ca2f61c03b
1 changed files with 117 additions and 0 deletions
  1. 117 0
      cache/redis.go

+ 117 - 0
cache/redis.go

@@ -0,0 +1,117 @@
+package cache
+
+import (
+	"context"
+	"fmt"
+	"log"
+	"os"
+	"strconv"
+	"strings"
+	"time"
+
+	"github.com/go-redis/redis/v8"
+
+	jsoniter "github.com/json-iterator/go"
+)
+
+var json = jsoniter.ConfigCompatibleWithStandardLibrary
+
+type Cache struct {
+	client *redis.Client
+	prefix string
+}
+
+type ChannelUserCache struct {
+	ID               int64
+	Name             string
+	Permission       []string `json:"permission"`
+	IsTemp           bool     `json:"isTemp"`
+	ValidArchivesIds []string `json:"validArchivesIds"`
+}
+
+type ManagerCache struct {
+	ID int64
+}
+
+type MemberCache struct {
+	ID   int64
+	Name string
+}
+
+var cache = &Cache{}
+
+func Instance() *Cache {
+	return cache
+}
+
+func GetClient() *redis.Client {
+	return cache.client
+}
+
+func InitRedis() *redis.Client {
+	RedisDB, err := strconv.Atoi(os.Getenv("REDIS_DB"))
+	if err != nil {
+		log.Panic("Redis数据库错误", err)
+	}
+	// 初始化Cache
+	return InitRedisClient(fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT")), os.Getenv("REDIS_PASSWORD"), "lumen_cache:", RedisDB)
+}
+
+func InitRedisClient(addr, password, p string, db int) *redis.Client {
+	client := redis.NewClient(&redis.Options{
+		Addr:     addr,
+		Password: password,
+		DB:       db,
+	})
+
+	if err := client.Ping(context.Background()).Err(); err != nil {
+		log.Panic("start cache", err)
+	}
+	cache.prefix = p
+	cache.client = client
+	return client
+}
+
+func (c *Cache) KeyWithPrefix(key string) string {
+	return c.prefix + key
+}
+
+func (c *Cache) Get(key string) (string, error) {
+	value, err := c.client.Get(context.Background(), c.KeyWithPrefix(key)).Result()
+	if err != nil {
+		return "", err
+	}
+	// PHP序列化之后的对象无法直接转为go的数据结构,直接都当数组处理
+	value = strings.ReplaceAll(value, `O:8:"stdClass"`, `a`)
+	value = strings.ReplaceAll(value, `O:17:"App\Models\Member"`, `a`)
+	value = strings.ReplaceAll(value, `O:15:"App\Models\User"`, `a`)
+	return value, nil
+}
+
+func (c *Cache) Put(key string, value interface{}, expiration time.Duration) error {
+	valueStr, err := json.MarshalToString(value)
+	if err != nil {
+		return err
+	}
+	ok, err := c.client.SetEX(context.Background(), c.KeyWithPrefix(key), valueStr, expiration).Result()
+	fmt.Println(ok, err)
+	return err
+}
+func (c *Cache) PutStrForever(key string, value string) error {
+	_, err := c.client.Set(context.Background(), c.KeyWithPrefix(key), value, 0).Result()
+	return err
+}
+
+func (c *Cache) PutStr(key string, value string, expiration time.Duration) error {
+	ok, err := c.client.SetEX(context.Background(), c.KeyWithPrefix(key), value, expiration).Result()
+	fmt.Println(ok, err)
+	return err
+}
+
+func (c *Cache) Delete(key string) (int64, error) {
+	return c.client.Del(context.Background(), c.KeyWithPrefix(key)).Result()
+}
+
+func (c *Cache) Incr(key string) (int64, error) {
+	return c.client.Incr(context.Background(), c.KeyWithPrefix(key)).Result()
+}