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

Method GetOrSet

cache.go:150–161  ·  view source on GitHub ↗

GetOrSet returns existing value or if record doesn't exist it sets a new key, value and expiration for a cache entry and stores it in the cache, returns nil in that case

(key, value []byte, expireSeconds int)

Source from the content-addressed store, hash-verified

148// GetOrSet returns existing value or if record doesn't exist
149// it sets a new key, value and expiration for a cache entry and stores it in the cache, returns nil in that case
150func (cache *Cache) GetOrSet(key, value []byte, expireSeconds int) (retValue []byte, err error) {
151 hashVal := hashFunc(key)
152 segID := hashVal & segmentAndOpVal
153 cache.locks[segID].Lock()
154 defer cache.locks[segID].Unlock()
155
156 retValue, _, err = cache.segments[segID].get(key, nil, hashVal, false)
157 if err != nil {
158 err = cache.segments[segID].set(key, value, hashVal, expireSeconds)
159 }
160 return
161}
162
163// SetAndGet sets a key, value and expiration for a cache entry and stores it in the cache.
164// If the key is larger than 65535 or value is larger than 1/1024 of the cache size,

Callers 1

TestGetOrSetFunction · 0.95

Calls 3

hashFuncFunction · 0.85
getMethod · 0.80
setMethod · 0.80

Tested by 1

TestGetOrSetFunction · 0.76