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.
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package store
|
|
|
|
import (
|
|
"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
|
|
|
|
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 = viper.GetString("redis.prefix")
|
|
} else {
|
|
bigcacheClient, _ := bigcache.NewBigCache(bigcache.DefaultConfig(14 * 24 * time.Hour))
|
|
bigcacheStore := bigcache_store.NewBigcache(bigcacheClient)
|
|
|
|
CacheStore = bigcacheStore
|
|
}
|
|
}
|
|
|
|
func NewManager[T any]() *cache.Cache[T] {
|
|
return cache.New[T](CacheStore)
|
|
}
|
|
|
|
func MakeKey(key string) string {
|
|
return CachePrefix + key
|
|
}
|