blob: 2e6ffaab35dd746221c360b1ebfd209c8246fc13 [file] [log] [blame]
Akrone73bc912025-06-17 16:36:45 +02001package config
2
3import (
4 "fmt"
5 "time"
6)
7
8// CacheConfig configures the cache behavior
9type CacheConfig struct {
10 // Enabled controls whether caching is active
11 Enabled bool `yaml:"enabled" default:"true" help:"Enable response caching"`
12
13 // DefaultTTL is the default time-to-live for cache entries
14 DefaultTTL string `yaml:"default_ttl" default:"5m" help:"Default cache TTL (e.g., '5m', '1h')"`
15
16 // SearchTTL is the TTL for search results
17 SearchTTL string `yaml:"search_ttl" default:"2m" help:"Cache TTL for search results (e.g., '2m', '30s')"`
18
19 // MetadataTTL is the TTL for metadata and corpus information
20 MetadataTTL string `yaml:"metadata_ttl" default:"15m" help:"Cache TTL for metadata and corpus info (e.g., '15m', '1h')"`
21
22 // MaxSize is the maximum number of cache entries
23 MaxSize int `yaml:"max_size" default:"1000" help:"Maximum number of cache entries"`
24}
25
26// DefaultCacheConfig returns a default cache configuration
27func DefaultCacheConfig() *CacheConfig {
28 return &CacheConfig{
29 Enabled: true,
30 DefaultTTL: "5m", // 5 minutes
31 SearchTTL: "2m", // 2 minutes - search results change less frequently
32 MetadataTTL: "15m", // 15 minutes - metadata is more stable
33 MaxSize: 1000,
34 }
35}
36
37// Validate validates cache configuration
38func (c *CacheConfig) Validate() error {
39 if !c.Enabled {
40 return nil // Skip validation if cache is disabled
41 }
42
43 if c.MaxSize <= 0 {
44 return fmt.Errorf("cache max_size must be positive, got %d", c.MaxSize)
45 }
46
47 // Validate TTL duration strings
48 if _, err := time.ParseDuration(c.DefaultTTL); err != nil {
49 return fmt.Errorf("invalid default_ttl '%s': %w", c.DefaultTTL, err)
50 }
51
52 if _, err := time.ParseDuration(c.SearchTTL); err != nil {
53 return fmt.Errorf("invalid search_ttl '%s': %w", c.SearchTTL, err)
54 }
55
56 if _, err := time.ParseDuration(c.MetadataTTL); err != nil {
57 return fmt.Errorf("invalid metadata_ttl '%s': %w", c.MetadataTTL, err)
58 }
59
60 return nil
61}
62
63// GetDefaultTTL returns the default TTL as a time.Duration
64func (c *CacheConfig) GetDefaultTTL() time.Duration {
65 if !c.Enabled {
66 return 0
67 }
68
69 duration, err := time.ParseDuration(c.DefaultTTL)
70 if err != nil {
71 return 5 * time.Minute // fallback to default
72 }
73 return duration
74}
75
76// GetSearchTTL returns the search TTL as a time.Duration
77func (c *CacheConfig) GetSearchTTL() time.Duration {
78 if !c.Enabled {
79 return 0
80 }
81
82 duration, err := time.ParseDuration(c.SearchTTL)
83 if err != nil {
84 return 2 * time.Minute // fallback to default
85 }
86 return duration
87}
88
89// GetMetadataTTL returns the metadata TTL as a time.Duration
90func (c *CacheConfig) GetMetadataTTL() time.Duration {
91 if !c.Enabled {
92 return 0
93 }
94
95 duration, err := time.ParseDuration(c.MetadataTTL)
96 if err != nil {
97 return 15 * time.Minute // fallback to default
98 }
99 return duration
100}