Files
mayfly-go/server/pkg/cache/global.go
2025-04-23 20:36:32 +08:00

47 lines
824 B
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 (
"mayfly-go/pkg/rediscli"
"time"
)
var c Cache = NewLocalCache()
// SetCache 设置全局缓存实现
func SetCache(cache Cache) {
c = cache
}
func GetStr(key string) string {
if val, ok := c.GetStr(key); ok {
return val
}
return ""
}
func GetInt(key string) int {
if val, ok := c.GetInt(key); ok {
return val
}
return 0
}
// Get 获取缓存值并使用json反序列化。返回是否获取成功。若不存在或者解析失败则返回false
func Get[T any](key string, valPtr T) bool {
return c.GetJson(key, valPtr)
}
// Set 设置缓存值
func Set(key string, value any, duration time.Duration) error {
return c.Set2Str(key, value, duration)
}
// 删除指定key
func Del(key string) {
c.Delete(key)
}
func UseRedisCache() bool {
return rediscli.GetCli() != nil
}