feat(cache): add in-memory cache service (ADR-0022 Phase 1 part 2) (#23)
All checks were successful
CI/CD Pipeline / Build Docker Cache (push) Successful in 50s
CI/CD Pipeline / CI Pipeline (push) Successful in 4m22s
CI/CD Pipeline / Trigger Docker Push (push) Successful in 6s

Phase 1 part 2 of ADR-0022 (companion to PR #22 rate-limit). In-memory cache service via go-cache, used by /api/version (60s TTL).

6/6 unit tests pass. ~95% Mistral autonomous via ICM workspace, cost €2.50 stages 01-02 (50% reduction vs T5 thanks to pre-extracted snippets in shared/).

Co-authored-by: Gabriel Radureau <arcodange@gmail.com>
Co-committed-by: Gabriel Radureau <arcodange@gmail.com>
This commit was merged in pull request #23.
This commit is contained in:
2026-05-03 13:24:17 +02:00
committed by arcodange
parent 54dd0cc80f
commit 358e3df38b
7 changed files with 296 additions and 14 deletions

56
pkg/cache/cache.go vendored Normal file
View File

@@ -0,0 +1,56 @@
package cache
import (
"time"
gocache "github.com/patrickmn/go-cache"
)
// Service defines the interface for cache operations
type Service interface {
Set(key string, value interface{}, ttl time.Duration)
Get(key string) (interface{}, bool)
Delete(key string)
Flush()
ItemCount() int
}
// InMemoryService implements Service using go-cache library
type InMemoryService struct {
cache *gocache.Cache
}
// NewInMemoryService creates a new in-memory cache service
// defaultTTL: default time-to-live for cache items
// cleanupInterval: interval at which expired items are cleaned up
func NewInMemoryService(defaultTTL, cleanupInterval time.Duration) Service {
c := gocache.New(defaultTTL, cleanupInterval)
return &InMemoryService{cache: c}
}
// Set stores a value in the cache with the specified TTL
func (s *InMemoryService) Set(key string, value interface{}, ttl time.Duration) {
s.cache.Set(key, value, ttl)
}
// Get retrieves a value from the cache
// Returns the value and true if found, nil and false if not found or expired
func (s *InMemoryService) Get(key string) (interface{}, bool) {
val, found := s.cache.Get(key)
return val, found
}
// Delete removes an item from the cache
func (s *InMemoryService) Delete(key string) {
s.cache.Delete(key)
}
// Flush clears all items from the cache
func (s *InMemoryService) Flush() {
s.cache.Flush()
}
// ItemCount returns the number of items currently in the cache
func (s *InMemoryService) ItemCount() int {
return s.cache.ItemCount()
}

135
pkg/cache/cache_test.go vendored Normal file
View File

@@ -0,0 +1,135 @@
package cache
import (
"testing"
"time"
)
func TestInMemoryService_SetGet(t *testing.T) {
svc := NewInMemoryService(1*time.Hour, 1*time.Hour)
// Test Set and Get
svc.Set("key1", "value1", 1*time.Hour)
val, ok := svc.Get("key1")
if !ok {
t.Fatal("Expected to find key1 in cache")
}
if val != "value1" {
t.Fatalf("Expected 'value1', got '%v'", val)
}
// Test Get non-existent key
_, ok = svc.Get("nonexistent")
if ok {
t.Fatal("Expected not to find nonexistent key")
}
}
func TestInMemoryService_Delete(t *testing.T) {
svc := NewInMemoryService(1*time.Hour, 1*time.Hour)
svc.Set("key1", "value1", 1*time.Hour)
_, ok := svc.Get("key1")
if !ok {
t.Fatal("Expected to find key1 before delete")
}
svc.Delete("key1")
_, ok = svc.Get("key1")
if ok {
t.Fatal("Expected not to find key1 after delete")
}
}
func TestInMemoryService_Flush(t *testing.T) {
svc := NewInMemoryService(1*time.Hour, 1*time.Hour)
svc.Set("key1", "value1", 1*time.Hour)
svc.Set("key2", "value2", 1*time.Hour)
if svc.ItemCount() != 2 {
t.Fatalf("Expected 2 items, got %d", svc.ItemCount())
}
svc.Flush()
if svc.ItemCount() != 0 {
t.Fatalf("Expected 0 items after flush, got %d", svc.ItemCount())
}
_, ok := svc.Get("key1")
if ok {
t.Fatal("Expected key1 to be flushed")
}
}
func TestInMemoryService_ItemCount(t *testing.T) {
svc := NewInMemoryService(1*time.Hour, 1*time.Hour)
if svc.ItemCount() != 0 {
t.Fatalf("Expected 0 items initially, got %d", svc.ItemCount())
}
svc.Set("key1", "value1", 1*time.Hour)
if svc.ItemCount() != 1 {
t.Fatalf("Expected 1 item, got %d", svc.ItemCount())
}
svc.Set("key2", "value2", 1*time.Hour)
if svc.ItemCount() != 2 {
t.Fatalf("Expected 2 items, got %d", svc.ItemCount())
}
svc.Delete("key1")
if svc.ItemCount() != 1 {
t.Fatalf("Expected 1 item after delete, got %d", svc.ItemCount())
}
}
func TestInMemoryService_TTLExpiration(t *testing.T) {
// Use a very short TTL for testing
svc := NewInMemoryService(100*time.Millisecond, 50*time.Millisecond)
svc.Set("key1", "value1", 50*time.Millisecond)
// Should be present immediately
val, ok := svc.Get("key1")
if !ok {
t.Fatal("Expected to find key1 immediately after set")
}
if val != "value1" {
t.Fatalf("Expected 'value1', got '%v'", val)
}
// Wait for expiration
time.Sleep(100 * time.Millisecond)
// Should be expired now
_, ok = svc.Get("key1")
if ok {
t.Fatal("Expected key1 to be expired after TTL")
}
}
func TestInMemoryService_DifferentTypes(t *testing.T) {
svc := NewInMemoryService(1*time.Hour, 1*time.Hour)
// Test with different types
svc.Set("string", "hello", 1*time.Hour)
svc.Set("int", 42, 1*time.Hour)
svc.Set("slice", []string{"a", "b"}, 1*time.Hour)
if svc.ItemCount() != 3 {
t.Fatalf("Expected 3 items, got %d", svc.ItemCount())
}
val, ok := svc.Get("string")
if !ok || val != "hello" {
t.Fatal("String value mismatch")
}
val, ok = svc.Get("int")
if !ok || val != 42 {
t.Fatal("Int value mismatch")
}
}

View File

@@ -28,6 +28,7 @@ type Config struct {
Auth AuthConfig `mapstructure:"auth"`
Database DatabaseConfig `mapstructure:"database"`
RateLimit RateLimitConfig `mapstructure:"rate_limit"`
Cache CacheConfig `mapstructure:"cache"`
}
// ServerConfig holds server-related configuration
@@ -105,6 +106,13 @@ type RateLimitConfig struct {
BurstSize int `mapstructure:"burst_size"`
}
// CacheConfig holds cache configuration
type CacheConfig struct {
Enabled bool `mapstructure:"enabled"`
DefaultTTLSeconds int `mapstructure:"default_ttl_seconds"`
CleanupIntervalSeconds int `mapstructure:"cleanup_interval_seconds"`
}
// VersionInfo holds application version information
type VersionInfo struct {
Version string `mapstructure:"-"` // Set via ldflags
@@ -202,6 +210,11 @@ func LoadConfig() (*Config, error) {
v.SetDefault("rate_limit.requests_per_minute", 60)
v.SetDefault("rate_limit.burst_size", 10)
// Cache defaults
v.SetDefault("cache.enabled", true)
v.SetDefault("cache.default_ttl_seconds", 300)
v.SetDefault("cache.cleanup_interval_seconds", 600)
// Auth defaults
v.SetDefault("auth.jwt_secret", "default-secret-key-please-change-in-production")
v.SetDefault("auth.admin_master_password", "admin123")
@@ -266,6 +279,11 @@ func LoadConfig() (*Config, error) {
v.BindEnv("rate_limit.requests_per_minute", "DLC_RATE_LIMIT_REQUESTS_PER_MINUTE")
v.BindEnv("rate_limit.burst_size", "DLC_RATE_LIMIT_BURST_SIZE")
// Cache environment variables
v.BindEnv("cache.enabled", "DLC_CACHE_ENABLED")
v.BindEnv("cache.default_ttl_seconds", "DLC_CACHE_DEFAULT_TTL_SECONDS")
v.BindEnv("cache.cleanup_interval_seconds", "DLC_CACHE_CLEANUP_INTERVAL_SECONDS")
// Database environment variables
v.BindEnv("database.host", "DLC_DATABASE_HOST")
v.BindEnv("database.port", "DLC_DATABASE_PORT")
@@ -428,6 +446,27 @@ func (c *Config) GetRateLimitBurstSize() int {
return c.RateLimit.BurstSize
}
// GetCacheEnabled returns whether cache is enabled
func (c *Config) GetCacheEnabled() bool {
return c.Cache.Enabled
}
// GetCacheDefaultTTLSeconds returns the default TTL in seconds for cache items
func (c *Config) GetCacheDefaultTTLSeconds() int {
if c.Cache.DefaultTTLSeconds <= 0 {
return 300
}
return c.Cache.DefaultTTLSeconds
}
// GetCacheCleanupIntervalSeconds returns the cleanup interval in seconds for cache
func (c *Config) GetCacheCleanupIntervalSeconds() int {
if c.Cache.CleanupIntervalSeconds <= 0 {
return 600
}
return c.Cache.CleanupIntervalSeconds
}
// GetDatabaseHost returns the database host
func (c *Config) GetDatabaseHost() string {
if c.Database.Host == "" {

View File

@@ -17,6 +17,7 @@ import (
"github.com/rs/zerolog/log"
httpSwagger "github.com/swaggo/http-swagger"
"dance-lessons-coach/pkg/cache"
"dance-lessons-coach/pkg/config"
"dance-lessons-coach/pkg/greet"
"dance-lessons-coach/pkg/middleware"
@@ -65,6 +66,7 @@ type Server struct {
validator *validation.Validator
userRepo user.UserRepository
userService user.UserService
cacheService cache.Service
startedAt time.Time
}
@@ -83,15 +85,28 @@ func NewServer(cfg *config.Config, readyCtx context.Context) *Server {
log.Warn().Err(err).Msg("Failed to initialize user services, user functionality will be disabled")
}
// Initialize cache service
var cacheService cache.Service
if cfg.GetCacheEnabled() {
cacheService = cache.NewInMemoryService(
time.Duration(cfg.GetCacheDefaultTTLSeconds())*time.Second,
time.Duration(cfg.GetCacheCleanupIntervalSeconds())*time.Second,
)
log.Trace().Msg("Cache service initialized")
} else {
log.Trace().Msg("Cache service disabled")
}
s := &Server{
router: chi.NewRouter(),
readyCtx: readyCtx,
withOTEL: cfg.GetTelemetryEnabled(),
config: cfg,
validator: validator,
userRepo: userRepo,
userService: userService,
startedAt: time.Now(),
router: chi.NewRouter(),
readyCtx: readyCtx,
withOTEL: cfg.GetTelemetryEnabled(),
config: cfg,
validator: validator,
userRepo: userRepo,
userService: userService,
cacheService: cacheService,
startedAt: time.Now(),
}
s.setupRoutes()
return s
@@ -351,26 +366,49 @@ func (s *Server) handleVersion(w http.ResponseWriter, r *http.Request) {
format = "plain" // default format
}
// Check cache if enabled
cacheKey := "version:" + format
if s.cacheService != nil {
if cached, ok := s.cacheService.Get(cacheKey); ok {
log.Trace().Str("cache_key", cacheKey).Msg("Cache hit for version")
w.Header().Set("Content-Type", "text/plain")
if format == "json" {
w.Header().Set("Content-Type", "application/json")
}
w.Write([]byte(cached.(string)))
return
}
}
// Build response
var response string
switch format {
case "plain":
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(version.Short()))
response = version.Short()
case "full":
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(version.Full()))
response = version.Full()
case "json":
w.Header().Set("Content-Type", "application/json")
jsonResponse := fmt.Sprintf(`{
response = fmt.Sprintf(`{
"version": "%s",
"commit": "%s",
"built": "%s",
"go": "%s"
}`, version.Version, version.Commit, version.Date, version.GoVersion)
w.Write([]byte(jsonResponse))
default:
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(version.Short()))
response = version.Short()
}
// Cache the response for 60 seconds if cache is enabled
if s.cacheService != nil {
s.cacheService.Set(cacheKey, response, 60*time.Second)
log.Trace().Str("cache_key", cacheKey).Msg("Cached version response")
}
w.Write([]byte(response))
}
// HealthzResponse represents the Kubernetes-style health check response