Skip to content

Commit 5e3716e

Browse files
cryo-zdrootfs
andauthored
feat: validate eviction policy in cache config (#223)
Signed-off-by: cryo <[email protected]> Co-authored-by: Huamin Chen <[email protected]>
1 parent 5f6e049 commit 5e3716e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/semantic-router/pkg/cache/cache_factory.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ func ValidateCacheConfig(config CacheConfig) error {
7474
if config.MaxEntries < 0 {
7575
return fmt.Errorf("max_entries cannot be negative for in-memory cache, got: %d", config.MaxEntries)
7676
}
77+
// Validate eviction policy
78+
switch config.EvictionPolicy {
79+
case "", FIFOEvictionPolicyType, LRUEvictionPolicyType, LFUEvictionPolicyType:
80+
// "" is allowed, treated as FIFO by default
81+
default:
82+
return fmt.Errorf("unsupported eviction_policy: %s", config.EvictionPolicy)
83+
}
7784
case MilvusCacheType:
7885
if config.BackendConfigPath == "" {
7986
return fmt.Errorf("backend_config_path is required for Milvus cache backend")

src/semantic-router/pkg/cache/cache_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ development:
220220
SimilarityThreshold: 0.8,
221221
MaxEntries: 1000,
222222
TTLSeconds: 3600,
223+
EvictionPolicy: "lru",
223224
}
224225

225226
err := cache.ValidateCacheConfig(config)
@@ -294,6 +295,21 @@ development:
294295
Expect(err.Error()).To(ContainSubstring("max_entries cannot be negative"))
295296
})
296297

298+
It("should return error for unsupported eviction_policy value in memory backend", func() {
299+
config := cache.CacheConfig{
300+
BackendType: cache.InMemoryCacheType,
301+
Enabled: true,
302+
SimilarityThreshold: 0.8,
303+
MaxEntries: 1000,
304+
TTLSeconds: 3600,
305+
EvictionPolicy: "random", // unsupported
306+
}
307+
308+
err := cache.ValidateCacheConfig(config)
309+
Expect(err).To(HaveOccurred())
310+
Expect(err.Error()).To(ContainSubstring("unsupported eviction_policy"))
311+
})
312+
297313
It("should return error for Milvus backend without config path", func() {
298314
config := cache.CacheConfig{
299315
BackendType: cache.MilvusCacheType,

0 commit comments

Comments
 (0)