add redis cache to competitions

This commit is contained in:
Timur Kh.
2025-12-17 12:24:07 +03:00
parent 8ded3cc6ab
commit 6a9376505d
5 changed files with 91 additions and 9 deletions
+17 -3
View File
@@ -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")
}