mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-19 11:36:57 +00:00
Move redis config to CacheConfig struct, add cache prefixes & trace logging
This commit is contained in:
parent
e1a22d5f4c
commit
46c8daacba
7 changed files with 45 additions and 30 deletions
26
server/cache/redis.go
vendored
26
server/cache/redis.go
vendored
|
@ -9,35 +9,41 @@ import (
|
|||
)
|
||||
|
||||
type RedisCache struct {
|
||||
ctx context.Context
|
||||
rdb *redis.Client
|
||||
name string
|
||||
ctx context.Context
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
func (r *RedisCache) Set(key string, value []byte, ttl time.Duration) error {
|
||||
return r.rdb.Set(r.ctx, key, value, ttl).Err()
|
||||
log.Trace().Str("key", r.name+"::"+key).Int("len(value)", len(value)).Bytes("value", value).Msg("Set in Redis.")
|
||||
return r.rdb.Set(r.ctx, r.name+"::"+key, value, ttl).Err()
|
||||
}
|
||||
|
||||
func (r *RedisCache) Get(key string) ([]byte, bool) {
|
||||
val, err := r.rdb.Get(r.ctx, key).Bytes()
|
||||
val, err := r.rdb.Get(r.ctx, r.name+"::"+key).Bytes()
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
log.Error().Err(err).Str("key", key).Msg("Couldn't request key from cache.")
|
||||
if !errors.Is(err, redis.Nil) {
|
||||
log.Error().Err(err).Str("key", r.name+"::"+key).Msg("Couldn't request key from cache.")
|
||||
} else {
|
||||
log.Trace().Str("key", r.name+"::"+key).Msg("Get from Redis, doesn't exist.")
|
||||
}
|
||||
return nil, false
|
||||
} else {
|
||||
log.Trace().Str("key", r.name+"::"+key).Int("len(value)", len(val)).Msg("Get from Redis.")
|
||||
return val, true
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RedisCache) Remove(key string) {
|
||||
err := r.rdb.Del(r.ctx, key).Err()
|
||||
if err == nil {
|
||||
log.Error().Err(err).Str("key", key).Msg("Couldn't delete key from cache.")
|
||||
err := r.rdb.Del(r.ctx, r.name+"::"+key).Err()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("key", r.name+"::"+key).Msg("Couldn't delete key from cache.")
|
||||
}
|
||||
}
|
||||
|
||||
func NewRedisCache(opts *redis.Options) ICache {
|
||||
func NewRedisCache(name string, opts *redis.Options) ICache {
|
||||
return &RedisCache{
|
||||
name,
|
||||
context.Background(),
|
||||
redis.NewClient(opts),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue