add redis cache to competitions
This commit is contained in:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user