- Implement cache-aside pattern via SearchCacheService in RouteSearch handler - Add TTL policies: 3 hours near-term, 7 days far-term - Write TestCacheAsideSearch and variants for TTL verification - Extend CacheAside to fully implement Cache interface (Get, Set, Exists, Delete, Increment, Decrement)
36 lines
897 B
Go
36 lines
897 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
"trip-planner/internal/cache"
|
|
"trip-planner/internal/routing"
|
|
"trip-planner/internal/yandex"
|
|
)
|
|
|
|
// HandlerContext holds the dependencies for API handlers.
|
|
type HandlerContext struct {
|
|
Cache cache.Cache
|
|
Redis *redis.Client
|
|
Router *routing.Graph
|
|
Yandex *yandex.Client
|
|
SearchCache *routing.SearchCacheService
|
|
}
|
|
|
|
// NewHandlerContext creates a new HandlerContext with initialized services.
|
|
func NewHandlerContext(redisClient *redis.Client, router *routing.Graph, yandex *yandex.Client) *HandlerContext {
|
|
cacheStore := cache.NewCacheStore(redisClient)
|
|
return &HandlerContext{
|
|
Cache: cacheStore,
|
|
Redis: redisClient,
|
|
Router: router,
|
|
Yandex: yandex,
|
|
SearchCache: routing.NewSearchCacheService(cache.NewCacheAside(cacheStore), yandex),
|
|
}
|
|
}
|