MCPcopy
hub / github.com/coocood/freecache / Update

Method Update

cache.go:188–206  ·  view source on GitHub ↗

Update gets value for a key, passes it to updater function that decides if set should be called as well This allows for an atomic Get plus Set call using the existing value to decide on whether to call Set. If the key is larger than 65535 or value is larger than 1/1024 of the cache size, the entry w

(key []byte, updater Updater)

Source from the content-addressed store, hash-verified

186// but it can be evicted when cache is full. Returns bool value to indicate if existing record was found along with bool
187// value indicating the value was replaced and error if any
188func (cache *Cache) Update(key []byte, updater Updater) (found bool, replaced bool, err error) {
189 hashVal := hashFunc(key)
190 segID := hashVal & segmentAndOpVal
191 cache.locks[segID].Lock()
192 defer cache.locks[segID].Unlock()
193
194 retValue, _, err := cache.segments[segID].get(key, nil, hashVal, false)
195 if err == nil {
196 found = true
197 } else {
198 err = nil // Clear ErrNotFound error since we're returning found flag
199 }
200 value, replaced, expireSeconds := updater(retValue, found)
201 if !replaced {
202 return
203 }
204 err = cache.segments[segID].set(key, value, hashVal, expireSeconds)
205 return
206}
207
208// Peek returns the value or not found error, without updating access time or counters.
209// Warning: No expiry check is performed so if an expired value is found, it will be

Callers 1

TestUpdateFunction · 0.95

Calls 3

hashFuncFunction · 0.85
getMethod · 0.80
setMethod · 0.80

Tested by 1

TestUpdateFunction · 0.76