package cache import ( "strings" "time" "github.com/patrickmn/go-cache" ) // 创建一个全局的不过期缓存对象 var cNoExpiration = cache.New(cache.NoExpiration, cache.NoExpiration) // 将数据放入缓存,并设置永不过期 func SetLocal(key string, value any) { cNoExpiration.Set(key, value, cache.NoExpiration) } // 从缓存中获取数据 func GetLocal(key string) (any, bool) { return cNoExpiration.Get(key) } // 从缓存中删除数据 func DeleteLocal(key string) { cNoExpiration.Delete(key) } // 获取指定前缀的所有键 func GetLocalKeys(prefix string) []string { prefix = strings.TrimSuffix(prefix, "*") var keys []string for key := range cNoExpiration.Items() { if strings.HasPrefix(key, prefix) { keys = append(keys, key) } } return keys } // 创建一个全局的过期缓存对象 var cTTL = cache.New(6*time.Hour, 12*time.Hour) // 设置具有过期时间的缓存项 func SetLocalTTL(key string, value any, expiration time.Duration) { cTTL.Set(key, value, expiration) } // 从缓存中获取数据 func GetLocalTTL(key string) (any, bool) { return cTTL.Get(key) } // 从缓存中删除数据 func DeleteLocalTTL(key string) { cTTL.Delete(key) }