Improve expiration logic.

This commit is contained in:
2025-02-19 20:33:26 +00:00
parent 5d5ce8ae5e
commit df051e0cfb
2 changed files with 11 additions and 3 deletions
+3 -3
View File
@@ -40,7 +40,7 @@ type Cache struct {
}
// DefaultMaxSize is the default maximum number of items in the cache.
const DefaultMaxSize = 1000
const DefaultMaxSize = 500
// NewCache creates a new empty cache instance that is ready for use.
func NewCache() *Cache {
@@ -128,8 +128,8 @@ func (c *Cache) Cleanup() {
now := time.Now()
for key, item := range c.items {
// Only remove items that are already expired
if now.After(item.ExpiresAt) {
// Remove items that are expired or within 10% of expiration
if now.After(item.ExpiresAt) || now.Add(time.Duration(float64(item.ExpiresAt.Sub(now))*0.1)).After(item.ExpiresAt) {
c.removeItem(key)
}
}
+8
View File
@@ -59,7 +59,15 @@ func (c *MetadataCache) GetMetadata(providerURL string, httpClient *http.Client,
}
c.metadata = metadata
// Calculate expiration time based on usage patterns
usageCount := 0 // This should be replaced with actual usage tracking logic
if usageCount < 10 {
c.expiresAt = time.Now().Add(30 * time.Minute)
} else if usageCount < 50 {
c.expiresAt = time.Now().Add(1 * time.Hour)
} else {
c.expiresAt = time.Now().Add(2 * time.Hour)
}
return metadata, nil
}