2021-06-07 17:22:07 +08:00
|
|
|
|
package cache
|
|
|
|
|
|
|
2025-04-23 20:36:32 +08:00
|
|
|
|
import (
|
2025-05-24 16:22:54 +08:00
|
|
|
|
"encoding/json"
|
2025-04-23 20:36:32 +08:00
|
|
|
|
"mayfly-go/pkg/utils/anyx"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/may-fly/cast"
|
|
|
|
|
|
)
|
2021-06-07 17:22:07 +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
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2025-04-23 20:36:32 +08:00
|
|
|
|
// Set2Str 将value转为字符串后设置缓存值
|
|
|
|
|
|
Set2Str(key string, value any, duration time.Duration) error
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2025-04-23 20:36:32 +08:00
|
|
|
|
// Get 获取缓存值
|
2023-06-01 12:31:32 +08:00
|
|
|
|
Get(k string) (any, bool)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2025-04-23 20:36:32 +08:00
|
|
|
|
// GetJson 获取json缓存值,并将其解析到指定的结构体指针
|
|
|
|
|
|
GetJson(k string, valPtr any) bool
|
|
|
|
|
|
|
|
|
|
|
|
// GetStr 获取字符串缓存值
|
|
|
|
|
|
GetStr(k string) (string, bool)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2025-04-23 20:36:32 +08:00
|
|
|
|
// GetInt 获取int缓存值
|
|
|
|
|
|
GetInt(k string) (int, bool)
|
2021-06-07 17:22:07 +08:00
|
|
|
|
|
2025-04-23 20:36:32 +08:00
|
|
|
|
// Delete 删除缓存
|
|
|
|
|
|
Delete(key string) error
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteByKeyPrefix 根据key前缀删除缓存
|
|
|
|
|
|
DeleteByKeyPrefix(keyPrefix string) error
|
|
|
|
|
|
|
|
|
|
|
|
// Count 缓存数量
|
|
|
|
|
|
Count() int
|
|
|
|
|
|
|
|
|
|
|
|
// Clear 清空所有缓存
|
2021-06-07 17:22:07 +08:00
|
|
|
|
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 {
|
2025-05-24 16:22:54 +08:00
|
|
|
|
json.Unmarshal([]byte(val), valPtr)
|
2025-04-23 20:36:32 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|