add redis cache to competitions
This commit is contained in:
@@ -21,6 +21,10 @@ type Config struct {
|
||||
DBPassword string
|
||||
DBName string
|
||||
JWTSecret string
|
||||
RedisAddr string
|
||||
RedisPassword string
|
||||
RedisDB int
|
||||
CacheEnabled bool
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
@@ -37,6 +41,10 @@ func Load() (*Config, error) {
|
||||
DBPassword: getEnv("POSTGRES_PASSWORD", "postgres"),
|
||||
DBName: getEnv("POSTGRES_DATABASE", "postgres"),
|
||||
JWTSecret: getEnv("JWT_SECRET", "your-secret-key-change-in-production"),
|
||||
RedisAddr: getEnv("REDIS_ADDR", "localhost:6379"),
|
||||
RedisPassword: getEnv("REDIS_PASSWORD", ""),
|
||||
RedisDB: mustGetInt("REDIS_DB", 0),
|
||||
CacheEnabled: mustGetBool("CACHE_ENABLED", true),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -11,18 +12,31 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
competitionCachePrefix = "competition:"
|
||||
)
|
||||
|
||||
type CompetitionRepository struct {
|
||||
db *sqlx.DB
|
||||
db *sqlx.DB
|
||||
redisClient *redis.Client
|
||||
cacheEnabled bool
|
||||
}
|
||||
|
||||
func NewCompetitionRepository(db *sqlx.DB) repository.CompetitionRepository {
|
||||
func NewCompetitionRepository(db *sqlx.DB, redisClient *redis.Client, cacheEnabled bool) repository.CompetitionRepository {
|
||||
return &CompetitionRepository{
|
||||
db: db,
|
||||
db: db,
|
||||
redisClient: redisClient,
|
||||
cacheEnabled: cacheEnabled,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) cacheKey(id string) string {
|
||||
return competitionCachePrefix + id
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) Create(ctx context.Context, c *pb.Competition) (*pb.Competition, error) {
|
||||
query := `INSERT INTO competitions (state, title, description, image_url, start_time, end_time, type, participation_type)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id, created_at, updated_at`
|
||||
@@ -45,14 +59,42 @@ func (r *CompetitionRepository) Create(ctx context.Context, c *pb.Competition) (
|
||||
c.CreatedAt = createdCompetition.CreatedAt
|
||||
c.UpdatedAt = createdCompetition.UpdatedAt
|
||||
|
||||
if r.cacheEnabled {
|
||||
data, err := json.Marshal(c)
|
||||
if err == nil {
|
||||
r.redisClient.Set(ctx, r.cacheKey(c.Id), data, 10*time.Minute).Err()
|
||||
}
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) Get(ctx context.Context, id uuid.UUID) (*pb.Competition, error) {
|
||||
if r.cacheEnabled {
|
||||
val, err := r.redisClient.Get(ctx, r.cacheKey(id.String())).Result()
|
||||
if err == nil {
|
||||
var competition pb.Competition
|
||||
if json.Unmarshal([]byte(val), &competition) == nil {
|
||||
return &competition, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
query := `SELECT id, state, title, description, image_url, start_time, end_time, type, participation_type, created_at, updated_at FROM competitions WHERE id = $1`
|
||||
var competition pb.Competition
|
||||
err := r.db.GetContext(ctx, &competition, query, id)
|
||||
return &competition, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r.cacheEnabled {
|
||||
data, err := json.Marshal(&competition)
|
||||
if err == nil {
|
||||
r.redisClient.Set(ctx, r.cacheKey(id.String()), data, 10*time.Minute).Err()
|
||||
}
|
||||
}
|
||||
|
||||
return &competition, nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) Update(ctx context.Context, c *pb.Competition) (*pb.Competition, error) {
|
||||
@@ -77,13 +119,25 @@ func (r *CompetitionRepository) Update(ctx context.Context, c *pb.Competition) (
|
||||
}
|
||||
c.UpdatedAt = updatedCompetition.UpdatedAt
|
||||
|
||||
if r.cacheEnabled {
|
||||
r.redisClient.Del(ctx, r.cacheKey(c.Id)).Err()
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
query := `DELETE FROM competitions WHERE id = $1`
|
||||
_, err := r.db.ExecContext(ctx, query, id)
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r.cacheEnabled {
|
||||
r.redisClient.Del(ctx, r.cacheKey(id.String())).Err()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCompetitionsOptions) ([]*pb.Competition, int, error) {
|
||||
@@ -149,5 +203,9 @@ func (r *CompetitionRepository) ChangeState(ctx context.Context, id uuid.UUID, s
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r.cacheEnabled {
|
||||
r.redisClient.Del(ctx, r.cacheKey(id.String())).Err()
|
||||
}
|
||||
|
||||
return r.Get(ctx, id)
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
@@ -26,8 +27,9 @@ const (
|
||||
|
||||
type Server struct {
|
||||
grpcServer *grpc.Server
|
||||
config *config.Config
|
||||
db *sqlx.DB
|
||||
config *config.Config
|
||||
db *sqlx.DB
|
||||
redisClient *redis.Client
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) *Server {
|
||||
@@ -43,6 +45,12 @@ func (s *Server) Start() error {
|
||||
}
|
||||
s.db = db
|
||||
|
||||
s.redisClient = redis.NewClient(&redis.Options{
|
||||
Addr: s.config.RedisAddr,
|
||||
Password: s.config.RedisPassword,
|
||||
DB: s.config.RedisDB,
|
||||
})
|
||||
|
||||
if err := s.registerGRPCServices(); err != nil {
|
||||
return fmt.Errorf("failed to register gRPC services: %w", err)
|
||||
}
|
||||
@@ -63,7 +71,7 @@ func (s *Server) Start() error {
|
||||
}
|
||||
|
||||
func (s *Server) registerGRPCServices() error {
|
||||
compRepo := postgres.NewCompetitionRepository(s.db)
|
||||
compRepo := postgres.NewCompetitionRepository(s.db, s.redisClient, s.config.CacheEnabled)
|
||||
compService := service.NewCompetitionService(compRepo)
|
||||
compHandler := grpcHandlers.NewCompetitionHandler(compService)
|
||||
|
||||
@@ -89,5 +97,11 @@ func (s *Server) Stop() {
|
||||
}
|
||||
}
|
||||
|
||||
if s.redisClient != nil {
|
||||
if err := s.redisClient.Close(); err != nil {
|
||||
log.Printf("failed to close redis client: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("competition server stopped")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user