feat(api): expose cache config snapshot

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 09:23:02 +00:00
parent 592cdd302e
commit f760ab6c72
4 changed files with 69 additions and 0 deletions

3
api.go
View file

@ -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

43
cache_config.go Normal file
View file

@ -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
}

View file

@ -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()

View file

@ -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))
}