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

106 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cache
import (
"encoding/json"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/rediscli"
"mayfly-go/pkg/utils/anyx"
"strings"
"time"
"github.com/may-fly/cast"
)
var tm *TimedCache
// 如果系统有设置redis信息则从redis获取否则本机内存获取
func GetStr(key string) string {
if !UseRedisCache() {
checkCache()
val, _ := tm.Get(key)
if val == nil {
return ""
}
return cast.ToString(val)
}
if res, err := rediscli.Get(key); err == nil {
return res
}
return ""
}
func GetInt(key string) int {
val := GetStr(key)
if val == "" {
return 0
}
return cast.ToInt(key)
}
// Get 获取缓存值并使用json反序列化。返回是否获取成功。若不存在或者解析失败则返回false
func Get[T any](key string, valPtr T) bool {
strVal := GetStr(key)
if strVal == "" {
return false
}
if err := json.Unmarshal([]byte(strVal), valPtr); err != nil {
logx.Errorf("json转换缓存中的值失败: %v", err)
return false
}
return true
}
// SetStr 如果系统有设置redis信息则使用redis存否则存于本机内存。duration == -1则为永久缓存
func SetStr(key, value string, duration time.Duration) error {
if !UseRedisCache() {
checkCache()
return tm.Add(key, value, duration)
}
return rediscli.Set(key, value, duration)
}
// Set 如果系统有设置redis信息则使用redis存否则存于本机内存。duration == -1则为永久缓存
func Set(key string, value any, duration time.Duration) error {
strVal := anyx.ToString(value)
if !UseRedisCache() {
checkCache()
return tm.Add(key, strVal, duration)
}
return rediscli.Set(key, strVal, duration)
}
// 删除指定key
func Del(key string) {
if !UseRedisCache() {
checkCache()
tm.Delete(key)
return
}
rediscli.Del(key)
}
// DelByKeyPrefix 根据key前缀删除满足前缀的所有缓存
func DelByKeyPrefix(keyPrefix string) error {
if !UseRedisCache() {
checkCache()
for key := range tm.Items() {
if strings.HasPrefix(cast.ToString(key), keyPrefix) {
tm.Delete(key)
}
}
return nil
}
return rediscli.DelByKeyPrefix(keyPrefix)
}
func UseRedisCache() bool {
return rediscli.GetCli() != nil
}
func checkCache() {
if tm == nil {
tm = NewTimedCache(time.Minute*time.Duration(5), 30*time.Second)
}
}