linstrom/storage/cache/cache.go
mStar 8709238859 Added goals, cache and func to get any remote obj
Cache is for storage, also includes pooled encoders and decoders
goals are things to eventually add to Linstrom
2024-09-06 23:01:57 +02:00

63 lines
1.7 KiB
Go

package cache
import (
"time"
"github.com/dgraph-io/ristretto"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/store"
redis_store "github.com/eko/gocache/store/redis/v4"
ristretto_store "github.com/eko/gocache/store/ristretto/v4"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/linstrom/config"
)
type Cache struct {
cache *cache.ChainCache[[]byte]
decoders *DecoderPool
encoders *EncoderPool
}
func NewCache() (*Cache, error) {
// ristretto is an in-memory cache
ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
// The *10 is a recommendation from ristretto
NumCounters: config.GlobalConfig.Storage.MaxInMemoryCacheSize * 10,
MaxCost: config.GlobalConfig.Storage.MaxInMemoryCacheSize,
BufferItems: 64, // Same here
})
if err != nil {
return nil, err
}
ristrettoStore := ristretto_store.NewRistretto(
ristrettoCache,
store.WithExpiration(time.Second*10),
)
var cacheManager *cache.ChainCache[[]byte]
if config.GlobalConfig.Storage.UseRedis {
if config.GlobalConfig.Storage.RedisUrl == nil {
log.Fatal().
Msg("Told to use redis in addition to in-memory store, but no redis url provided!")
}
opts, err := redis.ParseURL(*config.GlobalConfig.Storage.RedisUrl)
if err != nil {
return nil, err
}
redisClient := redis.NewClient(opts)
redisStore := redis_store.NewRedis(redisClient, store.WithExpiration(time.Minute))
cacheManager = cache.NewChain(
cache.New[[]byte](ristrettoStore),
cache.New[[]byte](redisStore),
)
} else {
cacheManager = cache.NewChain(cache.New[[]byte](ristrettoStore))
}
return &Cache{
cache: cacheManager,
decoders: NewDecoderPool(),
encoders: NewEncoderPool(),
}, nil
}