diff --git a/api.go b/api.go index 32a98a3..5770fbe 100644 --- a/api.go +++ b/api.go @@ -37,6 +37,9 @@ type Engine struct { addr string groups []RouteGroup middlewares []gin.HandlerFunc + cacheTTL time.Duration + cacheMaxEntries int + cacheMaxBytes int wsHandler http.Handler wsPath string sseBroker *SSEBroker diff --git a/cache_config.go b/cache_config.go new file mode 100644 index 0000000..726ee0c --- /dev/null +++ b/cache_config.go @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: EUPL-1.2 + +package api + +import "time" + +// CacheConfig captures the configured response cache settings for an Engine. +// +// It is intentionally small and serialisable so callers can inspect the active +// cache policy without needing to rebuild middleware state. +// +// Example: +// +// cfg := api.CacheConfig{Enabled: true, TTL: 5 * time.Minute} +type CacheConfig struct { + Enabled bool + TTL time.Duration + MaxEntries int + MaxBytes int +} + +// CacheConfig returns the currently configured response cache settings for the engine. +// +// The result snapshots the Engine state at call time. +// +// Example: +// +// cfg := engine.CacheConfig() +func (e *Engine) CacheConfig() CacheConfig { + if e == nil { + return CacheConfig{} + } + + cfg := CacheConfig{ + TTL: e.cacheTTL, + MaxEntries: e.cacheMaxEntries, + MaxBytes: e.cacheMaxBytes, + } + if e.cacheTTL > 0 { + cfg.Enabled = true + } + return cfg +} diff --git a/modernization_test.go b/modernization_test.go index 776d8f6..7255667 100644 --- a/modernization_test.go +++ b/modernization_test.go @@ -5,6 +5,7 @@ package api_test import ( "slices" "testing" + "time" api "dappco.re/go/core/api" ) @@ -94,6 +95,25 @@ func TestEngine_ChannelsIter_Good_SnapshotsCurrentChannels(t *testing.T) { } } +func TestEngine_CacheConfig_Good_SnapshotsCurrentSettings(t *testing.T) { + e, _ := api.New(api.WithCacheLimits(5*time.Minute, 10, 1024)) + + cfg := e.CacheConfig() + + if !cfg.Enabled { + t.Fatal("expected cache config to be enabled") + } + if cfg.TTL != 5*time.Minute { + t.Fatalf("expected TTL %v, got %v", 5*time.Minute, cfg.TTL) + } + if cfg.MaxEntries != 10 { + t.Fatalf("expected MaxEntries 10, got %d", cfg.MaxEntries) + } + if cfg.MaxBytes != 1024 { + t.Fatalf("expected MaxBytes 1024, got %d", cfg.MaxBytes) + } +} + func TestEngine_Register_Good_IgnoresNilGroups(t *testing.T) { e, _ := api.New() diff --git a/options.go b/options.go index 4c61289..e15f9d9 100644 --- a/options.go +++ b/options.go @@ -529,6 +529,9 @@ func WithCacheLimits(ttl time.Duration, maxEntries, maxBytes int) Option { if ttl <= 0 { return } + e.cacheTTL = ttl + e.cacheMaxEntries = maxEntries + e.cacheMaxBytes = maxBytes store := newCacheStore(maxEntries, maxBytes) e.middlewares = append(e.middlewares, cacheMiddleware(store, ttl)) }