forked from mtusi2026/mtusi2026lab2
реализован уровень 4
This commit is contained in:
+38
-1
@@ -10,6 +10,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
applicationCache "mtusi2026lab2/internal/cache"
|
||||
"mtusi2026lab2/internal/config"
|
||||
"mtusi2026lab2/internal/database"
|
||||
"mtusi2026lab2/internal/store"
|
||||
@@ -41,8 +42,44 @@ func main() {
|
||||
cfg.DatabaseQueryTimeout,
|
||||
)
|
||||
|
||||
var applicationStore store.Repository = postgresStore
|
||||
|
||||
if cfg.RedisURL != "" {
|
||||
redisChatCache, cacheError := applicationCache.NewRedisChatCache(
|
||||
cfg.RedisURL,
|
||||
cfg.RedisCacheTTL,
|
||||
cfg.RedisOperationTimeout,
|
||||
cfg.RedisPoolSize,
|
||||
)
|
||||
if cacheError != nil {
|
||||
log.Fatalf("[FATAL] Invalid Redis configuration: %v", cacheError)
|
||||
}
|
||||
defer func() {
|
||||
if closeError := redisChatCache.Close(); closeError != nil {
|
||||
log.Printf("[WARN] Redis client close failed: %v", closeError)
|
||||
}
|
||||
}()
|
||||
|
||||
cacheContext, cacheCancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
cfg.RedisOperationTimeout,
|
||||
)
|
||||
if cacheError = redisChatCache.Ping(cacheContext); cacheError != nil {
|
||||
log.Printf("[WARN] Redis unavailable at startup; PostgreSQL fallback is active: %v", cacheError)
|
||||
} else {
|
||||
log.Println("[INFO] Redis chat cache is available")
|
||||
}
|
||||
cacheCancel()
|
||||
|
||||
applicationStore = store.NewCachedRepository(
|
||||
postgresStore,
|
||||
redisChatCache,
|
||||
log.Default(),
|
||||
)
|
||||
}
|
||||
|
||||
handler := handlers.NewHandler(
|
||||
postgresStore,
|
||||
applicationStore,
|
||||
handlers.WithReadiness(
|
||||
databasePool,
|
||||
cfg.ReadinessTimeout,
|
||||
|
||||
Reference in New Issue
Block a user