Something went wrong. Try again.
notes and lists
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121package cache
import ( "sync" "time")
// CacheEntry stores a cached value with its expiration timetype CacheEntry struct { Value interface{} ExpiresAt time.Time AccessedAt time.Time // For LRU tracking}
// IsExpired checks if the cache entry has expiredfunc (e *CacheEntry) IsExpired() bool { return time.Now().After(e.ExpiresAt)}
// TTLCache is a thread-safe cache with TTL and LRU evictiontype TTLCache struct { mu sync.Mutex items map[string]*CacheEntry maxSize int defaultTTL time.Duration}
// NewTTLCache creates a new cache with the specified max size and default TTLfunc NewTTLCache(maxSize int, defaultTTL time.Duration) *TTLCache { return &TTLCache{ items: make(map[string]*CacheEntry), maxSize: maxSize, defaultTTL: defaultTTL, }}
// Get retrieves a value from the cache if it exists and hasn't expiredfunc (c *TTLCache) Get(key string) (interface{}, bool) { c.mu.Lock() defer c.mu.Unlock()
entry, exists := c.items[key] if !exists { return nil, false }
// Check if expired if entry.IsExpired() { // Delete expired entry delete(c.items, key) return nil, false }
// Update access time for LRU entry.AccessedAt = time.Now()
return entry.Value, true}
// Set stores a value in the cache with the default TTLfunc (c *TTLCache) Set(key string, value interface{}) { c.SetWithTTL(key, value, c.defaultTTL)}
// SetWithTTL stores a value with a specific TTLfunc (c *TTLCache) SetWithTTL(key string, value interface{}, ttl time.Duration) { c.mu.Lock() defer c.mu.Unlock()
// If at capacity and this is a new key, evict oldest if len(c.items) >= c.maxSize { if _, exists := c.items[key]; !exists { c.evictLRU() } }
c.items[key] = &CacheEntry{ Value: value, ExpiresAt: time.Now().Add(ttl), AccessedAt: time.Now(), }}
// evictLRU removes the least recently used entryfunc (c *TTLCache) evictLRU() { var oldestKey string var oldestTime time.Time
for key, entry := range c.items { if oldestKey == "" || entry.AccessedAt.Before(oldestTime) { oldestKey = key oldestTime = entry.AccessedAt } }
if oldestKey != "" { delete(c.items, oldestKey) }}
// Delete removes a specific key from the cachefunc (c *TTLCache) Delete(key string) { c.mu.Lock() defer c.mu.Unlock() delete(c.items, key)}
// Clear removes all items from the cachefunc (c *TTLCache) Clear() { c.mu.Lock() defer c.mu.Unlock() c.items = make(map[string]*CacheEntry)}
// Size returns the current number of items in the cachefunc (c *TTLCache) Size() int { c.mu.Lock() defer c.mu.Unlock() return len(c.items)}