FEAT: Added Redis Caching
Deploy Infrastructure / pulumi-up (push) Successful in 3s
Build, Push and Deploy Discovery / build-push-deploy (push) Failing after 34m37s

This commit is contained in:
FahdShalhoub
2026-08-29 23:12:11 +03:00
parent f66f3b555d
commit 35d4d6756e
8 changed files with 386 additions and 8 deletions
+58
View File
@@ -0,0 +1,58 @@
package services
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/redis/go-redis/v9"
)
var CacheClient Cache
var ErrCacheMiss = errors.New("cache: miss")
type Cache interface {
Get(ctx context.Context, key string) ([]byte, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
}
type RedisConcrete struct {
Redis *redis.Client
}
const cacheOperationTimeout = 100 * time.Millisecond
func (svc RedisConcrete) Get(ctx context.Context, key string) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, cacheOperationTimeout)
defer cancel()
value, err := svc.Redis.Get(ctx, key).Bytes()
if errors.Is(err, redis.Nil) {
return nil, ErrCacheMiss
}
if err != nil {
return nil, err
}
return value, nil
}
func (svc RedisConcrete) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, cacheOperationTimeout)
defer cancel()
return svc.Redis.Set(ctx, key, value, ttl).Err()
}
func (svc RedisConcrete) Close() error {
return svc.Redis.Close()
}
func (svc RedisConcrete) AssertSuccessfulConnection(ctx context.Context) {
if err := svc.Redis.Ping(ctx).Err(); err != nil {
log.Printf("WARNING: %s", fmt.Errorf("redis: cannot reach cache at %q, searches will be served from the database: %w",
svc.Redis.Options().Addr, err))
}
}