You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB
Go

package store
import (
"strings"
"time"
"github.com/allegro/bigcache"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/store"
bigcache_store "github.com/eko/gocache/store/bigcache/v4"
redis_store "github.com/eko/gocache/store/redis/v4"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
)
var CacheStore store.StoreInterface
var CachePrefix string
var PersistTTL = 14 * 24 * time.Hour
var CacheTTL = 12 * time.Hour
func InitStore() {
if viper.GetString("redis.addr") != "" {
redisStore := redis_store.NewRedis(redis.NewClient(&redis.Options{
Addr: viper.GetString("redis.addr"),
Password: viper.GetString("redis.password"),
Network: viper.GetString("redis.network"),
}))
CacheStore = redisStore
CachePrefix = strings.TrimRight(viper.GetString("redis.prefix"), ":")
} else {
bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(PersistTTL))
bigcacheStore := bigcache_store.NewBigcache(bigcacheClient)
CacheStore = bigcacheStore
}
}
func NewManager[T any]() *cache.Cache[T] {
return cache.New[T](CacheStore)
}
func MakeKey(keys ...string) string {
keys = append([]string{CachePrefix}, keys...)
return strings.Join(keys, ":")
}