-
|
There is no clear explanation of how the cache is linked or how it is used; I am not sure if I simply haven't figured out how to access it or if I just don't understand it. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
You are not missing a built-in Redis switch in the docs. In the current code, Limen has a generic That shared cache is used by cache-backed sessions, the rate limiter, and JWT blacklist storage. Redis support is expected to be plugged in by providing your own The shape is roughly: type RedisCacheStore struct {
client *redis.Client
}
func (r *RedisCacheStore) Get(ctx context.Context, key string) ([]byte, error) {
b, err := r.client.Get(ctx, key).Bytes()
if errors.Is(err, redis.Nil) {
return nil, limen.ErrRecordNotFound
}
return b, err
}
func (r *RedisCacheStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
return r.client.Set(ctx, key, value, ttl).Err()
}
func (r *RedisCacheStore) Has(ctx context.Context, key string) (bool, error) {
n, err := r.client.Exists(ctx, key).Result()
return n > 0, err
}
func (r *RedisCacheStore) Delete(ctx context.Context, key string) error {
return r.client.Del(ctx, key).Err()
}Then pass it in: app := limen.New(limen.Config{
Database: db,
Secret: secret,
CacheStore: redisStore,
CacheKeyPrefix: "myapp",
})For sessions specifically, also make sure the session config is using cache storage if that is what you want; the default session store type is database, while the rate limiter defaults to cache. |
Beta Was this translation helpful? Give feedback.
You are not missing a built-in Redis switch in the docs. In the current code, Limen has a generic
CacheAdapterinterface and defaults toNewMemoryCacheStore()whenConfig.CacheStoreis nil.That shared cache is used by cache-backed sessions, the rate limiter, and JWT blacklist storage. Redis support is expected to be plugged in by providing your own
Config.CacheStoreimplementation.The shape is roughly: