MCPcopy
hub / github.com/go-gorm/gorm / Add

Method Add

internal/lru/lru.go:118–144  ·  view source on GitHub ↗

Add adds a value to the cache. Returns true if an eviction occurred. Returns false if there was no eviction: the item was already in the cache, or the size was not exceeded.

(key K, value V)

Source from the content-addressed store, hash-verified

116// Returns false if there was no eviction: the item was already in the cache,
117// or the size was not exceeded.
118func (c *LRU[K, V]) Add(key K, value V) (evicted bool) {
119 c.mu.Lock()
120 defer c.mu.Unlock()
121 now := time.Now()
122
123 // Check for existing item
124 if ent, ok := c.items[key]; ok {
125 c.evictList.MoveToFront(ent)
126 c.removeFromBucket(ent) // remove the entry from its current bucket as expiresAt is renewed
127 ent.Value = value
128 ent.ExpiresAt = now.Add(c.ttl)
129 c.addToBucket(ent)
130 return false
131 }
132
133 // Add new item
134 ent := c.evictList.PushFrontExpirable(key, value, now.Add(c.ttl))
135 c.items[key] = ent
136 c.addToBucket(ent) // adds the entry to the appropriate bucket and sets entry.expireBucket
137
138 evict := c.size > 0 && c.evictList.Length() > c.size
139 // Verify size not exceeded
140 if evict {
141 c.removeOldest()
142 }
143 return evict
144}
145
146// Get looks up a key's value from the cache.
147func (c *LRU[K, V]) Get(key K) (value V, ok bool) {

Callers 15

TestAssertEqualFunction · 0.80
logMethod · 0.80
SetMethod · 0.80
TestPreloadGoroutineFunction · 0.80
TestPreparedStmtDeadlockFunction · 0.80
TestGenericsReuseFunction · 0.80

Calls 6

removeFromBucketMethod · 0.95
addToBucketMethod · 0.95
removeOldestMethod · 0.95
MoveToFrontMethod · 0.80
PushFrontExpirableMethod · 0.80
LengthMethod · 0.65