mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-19 11:36:57 +00:00
Move to []byte for caching and make it compile
This commit is contained in:
parent
5b6eecc75f
commit
c4181d1206
11 changed files with 63 additions and 43 deletions
4
server/cache/interface.go
vendored
4
server/cache/interface.go
vendored
|
@ -4,7 +4,7 @@ import "time"
|
|||
|
||||
// ICache is an interface that defines how the pages server interacts with the cache.
|
||||
type ICache interface {
|
||||
Set(key string, value string, ttl time.Duration) error
|
||||
Get(key string) (string, bool)
|
||||
Set(key string, value []byte, ttl time.Duration) error
|
||||
Get(key string) ([]byte, bool)
|
||||
Remove(key string)
|
||||
}
|
||||
|
|
8
server/cache/memory.go
vendored
8
server/cache/memory.go
vendored
|
@ -9,16 +9,16 @@ type MCache struct {
|
|||
mcache *mcache.CacheDriver
|
||||
}
|
||||
|
||||
func (m *MCache) Set(key string, value string, ttl time.Duration) error {
|
||||
func (m *MCache) Set(key string, value []byte, ttl time.Duration) error {
|
||||
return m.mcache.Set(key, value, ttl)
|
||||
}
|
||||
|
||||
func (m *MCache) Get(key string) (string, bool) {
|
||||
func (m *MCache) Get(key string) ([]byte, bool) {
|
||||
val, ok := m.mcache.Get(key)
|
||||
if ok {
|
||||
return val.(string), true
|
||||
return val.([]byte), true
|
||||
} else {
|
||||
return "", false
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
|
|
11
server/cache/redis.go
vendored
11
server/cache/redis.go
vendored
|
@ -2,6 +2,7 @@ package cache
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/rs/zerolog/log"
|
||||
"time"
|
||||
|
@ -12,17 +13,17 @@ type RedisCache struct {
|
|||
rdb *redis.Client
|
||||
}
|
||||
|
||||
func (r *RedisCache) Set(key string, value string, ttl time.Duration) error {
|
||||
func (r *RedisCache) Set(key string, value []byte, ttl time.Duration) error {
|
||||
return r.rdb.Set(r.ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
func (r *RedisCache) Get(key string) (string, bool) {
|
||||
val, err := r.rdb.Get(r.ctx, key).Result()
|
||||
func (r *RedisCache) Get(key string) ([]byte, bool) {
|
||||
val, err := r.rdb.Get(r.ctx, key).Bytes()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
log.Error().Err(err).Str("key", key).Msg("Couldn't request key from cache.")
|
||||
}
|
||||
return "", false
|
||||
return nil, false
|
||||
} else {
|
||||
return val, true
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue