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)
| 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 |
| 150 | func (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, |