Files
Atlas/internal/store/cache.go
T
renjue 8eacbb2b3e
CI / docker (push) Successful in 3m7s
实现 Atlas AI 平台一期能力并完成品牌化改造。
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 23:01:52 +08:00

47 lines
801 B
Go

package store
import (
"sync"
"time"
)
type HotCache struct {
mu sync.RWMutex
entries map[string]cacheEntry
ttl time.Duration
}
type cacheEntry struct {
value any
expiresAt time.Time
}
func NewHotCache(ttl time.Duration) *HotCache {
return &HotCache{
entries: make(map[string]cacheEntry),
ttl: ttl,
}
}
func (c *HotCache) Get(key string) (any, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
e, ok := c.entries[key]
if !ok || time.Now().After(e.expiresAt) {
return nil, false
}
return e.value, true
}
func (c *HotCache) Set(key string, value any) {
c.mu.Lock()
defer c.mu.Unlock()
c.entries[key] = cacheEntry{value: value, expiresAt: time.Now().Add(c.ttl)}
}
func (c *HotCache) Delete(key string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.entries, key)
}