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

Method SetAndGet

cache.go:168–180  ·  view source on GitHub ↗

SetAndGet sets a key, value and expiration for a cache entry and stores it in the cache. If the key is larger than 65535 or value is larger than 1/1024 of the cache size, the entry will not be written to the cache. expireSeconds <= 0 means no expire, but it can be evicted when cache is full. Return

(key, value []byte, expireSeconds int)

Source from the content-addressed store, hash-verified

166// but it can be evicted when cache is full. Returns existing value if record exists
167// with a bool value to indicate whether an existing record was found
168func (cache *Cache) SetAndGet(key, value []byte, expireSeconds int) (retValue []byte, found bool, err error) {
169 hashVal := hashFunc(key)
170 segID := hashVal & segmentAndOpVal
171 cache.locks[segID].Lock()
172 defer cache.locks[segID].Unlock()
173
174 retValue, _, err = cache.segments[segID].get(key, nil, hashVal, false)
175 if err == nil {
176 found = true
177 }
178 err = cache.segments[segID].set(key, value, hashVal, expireSeconds)
179 return
180}
181
182// Update gets value for a key, passes it to updater function that decides if set should be called as well
183// This allows for an atomic Get plus Set call using the existing value to decide on whether to call Set.

Callers 1

TestSetAndGetFunction · 0.95

Calls 3

hashFuncFunction · 0.85
getMethod · 0.80
setMethod · 0.80

Tested by 1

TestSetAndGetFunction · 0.76