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
This commit is contained in:
Melody Becker 2024-09-06 23:01:57 +02:00
parent 2977f09245
commit 8709238859
8 changed files with 907 additions and 16 deletions

63
storage/cache/cache.go vendored Normal file
View file

@ -0,0 +1,63 @@
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
}