Add cache configuration to cli
Change-Id: I22b7eb9bd9a2fb9e7106ff20487e285638c5320f
diff --git a/service/cache.go b/service/cache.go
index 43ec439..00b3abd 100644
--- a/service/cache.go
+++ b/service/cache.go
@@ -9,7 +9,7 @@
"strings"
"time"
- cfg "github.com/korap/korap-mcp/config"
+ "github.com/korap/korap-mcp/config"
"github.com/korap/korap-mcp/logger"
"github.com/maypok86/otter"
"github.com/rs/zerolog"
@@ -31,54 +31,29 @@
type Cache struct {
cache *otter.Cache[string, *CacheEntry]
logger zerolog.Logger
- config CacheConfig
+ config *config.CacheConfig
}
-// CacheConfig configures the cache behavior
-type CacheConfig struct {
- // Enabled controls whether caching is active
- Enabled bool
- // DefaultTTL is the default time-to-live for cache entries
- DefaultTTL time.Duration
- // SearchTTL is the TTL for search results
- SearchTTL time.Duration
- // MetadataTTL is the TTL for metadata and corpus information
- MetadataTTL time.Duration
- // MaxSize is the maximum number of cache entries
- MaxSize int
-}
-
-// DefaultCacheConfig returns a default cache configuration
-func DefaultCacheConfig() CacheConfig {
- return CacheConfig{
- Enabled: true,
- DefaultTTL: 5 * time.Minute,
- SearchTTL: 2 * time.Minute, // Search results change less frequently
- MetadataTTL: 15 * time.Minute, // Metadata is more stable
- MaxSize: 1000,
- }
-}
-
-// NewCache creates a new cache instance
-func NewCache(config CacheConfig) (*Cache, error) {
+// NewCache creates a new cache instance from config.CacheConfig
+func NewCache(cacheConfig *config.CacheConfig) (*Cache, error) {
// Create default logging config for cache
- logConfig := &cfg.LoggingConfig{
+ logConfig := &config.LoggingConfig{
Level: "info",
Format: "text",
}
- if !config.Enabled {
+ if !cacheConfig.Enabled {
return &Cache{
cache: nil,
logger: logger.GetLogger(logConfig),
- config: config,
+ config: cacheConfig,
}, nil
}
// Create otter cache with specified capacity
- cache, err := otter.MustBuilder[string, *CacheEntry](config.MaxSize).
+ cache, err := otter.MustBuilder[string, *CacheEntry](cacheConfig.MaxSize).
CollectStats().
- WithTTL(config.DefaultTTL).
+ WithTTL(cacheConfig.GetDefaultTTL()).
Build()
if err != nil {
return nil, fmt.Errorf("failed to create cache: %w", err)
@@ -87,7 +62,7 @@
return &Cache{
cache: &cache,
logger: logger.GetLogger(logConfig),
- config: config,
+ config: cacheConfig,
}, nil
}
@@ -188,9 +163,9 @@
"hit_ratio": stats.Ratio(),
"evictions": stats.EvictedCount(),
"max_size": c.config.MaxSize,
- "default_ttl": c.config.DefaultTTL.String(),
- "search_ttl": c.config.SearchTTL.String(),
- "metadata_ttl": c.config.MetadataTTL.String(),
+ "default_ttl": c.config.DefaultTTL,
+ "search_ttl": c.config.SearchTTL,
+ "metadata_ttl": c.config.MetadataTTL,
}
}
@@ -200,17 +175,17 @@
// Search endpoints get shorter TTL
if strings.Contains(endpoint, "search") || strings.Contains(endpoint, "query") {
- return c.config.SearchTTL
+ return c.config.GetSearchTTL()
}
// Metadata and corpus endpoints get longer TTL
if strings.Contains(endpoint, "corpus") || strings.Contains(endpoint, "metadata") ||
strings.Contains(endpoint, "statistics") || strings.Contains(endpoint, "info") {
- return c.config.MetadataTTL
+ return c.config.GetMetadataTTL()
}
// Default TTL for other endpoints
- return c.config.DefaultTTL
+ return c.config.GetDefaultTTL()
}
// Close closes the cache and cleans up resources