MCPcopy Create free account
hub / github.com/astercloud/aster / setToMemory

Method setToMemory

pkg/tools/cache.go:258–281  ·  view source on GitHub ↗
(key string, value any, ttl time.Duration)

Source from the content-addressed store, hash-verified

256}
257
258func (c *ToolCache) setToMemory(key string, value any, ttl time.Duration) {
259 c.memoryMu.Lock()
260 defer c.memoryMu.Unlock()
261
262 // 检查是否需要驱逐
263 if c.config.MaxMemoryItems > 0 && len(c.memoryCache) >= c.config.MaxMemoryItems {
264 c.evictOldest()
265 }
266
267 // 计算大小(粗略估计)
268 size := int64(len(fmt.Sprintf("%v", value)))
269
270 entry := &CacheEntry{
271 Key: key,
272 Value: value,
273 CreatedAt: time.Now(),
274 ExpiresAt: time.Now().Add(ttl),
275 Size: size,
276 }
277
278 c.memoryCache[key] = entry
279 c.stats.ItemCount++
280 c.stats.TotalSize += size
281}
282
283func (c *ToolCache) deleteFromMemory(key string) {
284 c.memoryMu.Lock()

Callers 2

GetMethod · 0.95
SetMethod · 0.95

Calls 2

evictOldestMethod · 0.95
AddMethod · 0.65

Tested by

no test coverage detected