Files
mayfly-go/server/pkg/cache/cache.go

73 lines
1.6 KiB
Go
Raw Normal View History

package cache
2025-04-23 20:36:32 +08:00
import (
"encoding/json"
2025-04-23 20:36:32 +08:00
"mayfly-go/pkg/utils/anyx"
"time"
2025-06-27 12:17:45 +08:00
"github.com/spf13/cast"
2025-04-23 20:36:32 +08:00
)
2025-04-23 20:36:32 +08:00
type Cache interface {
// Set 设置缓存值
// duration == -1 则为永久缓存
Set(key string, value any, duration time.Duration) error
2025-04-23 20:36:32 +08:00
// Set2Str 将value转为字符串后设置缓存值
Set2Str(key string, value any, duration time.Duration) error
2025-04-23 20:36:32 +08:00
// Get 获取缓存值
Get(k string) (any, bool)
2025-04-23 20:36:32 +08:00
// GetJson 获取json缓存值并将其解析到指定的结构体指针
GetJson(k string, valPtr any) bool
// GetStr 获取字符串缓存值
GetStr(k string) (string, bool)
2025-04-23 20:36:32 +08:00
// GetInt 获取int缓存值
GetInt(k string) (int, bool)
2025-04-23 20:36:32 +08:00
// Delete 删除缓存
Delete(key string) error
// DeleteByKeyPrefix 根据key前缀删除缓存
DeleteByKeyPrefix(keyPrefix string) error
// Count 缓存数量
Count() int
// Clear 清空所有缓存
Clear()
}
2025-04-23 20:36:32 +08:00
type defaultCache struct {
c Cache // 具体的缓存实现, 用于实现一些接口的通用方法
}
func (dc *defaultCache) Set2Str(key string, value any, duration time.Duration) error {
return dc.c.Set(key, anyx.ToString(value), duration)
}
func (dc *defaultCache) GetStr(k string) (string, bool) {
if val, ok := dc.c.Get(k); ok {
return cast.ToString(val), true
}
return "", false
}
func (dc *defaultCache) GetInt(k string) (int, bool) {
if val, ok := dc.c.Get(k); ok {
return cast.ToInt(val), true
}
return 0, false
}
func (dc *defaultCache) GetJson(k string, valPtr any) bool {
if val, ok := dc.GetStr(k); ok {
json.Unmarshal([]byte(val), valPtr)
2025-04-23 20:36:32 +08:00
return true
}
return false
}