GetOrSet

Functionin simplecompute-sdk-go v1.11.0View source
func GetOrSet(key []byte, setFn func() (CacheEntry, error)) (io.ReadCloser, error)

GetOrSet retrieves the object stored in the cache for the given key if it exists, or inserts and returns the contents by running the provided setFn function.

The setFn function is only run when no value is present for the key, and no other client is in the process of setting it. The function should return a populated CacheEntry or an error.

If the setFn function returns an error, nothing will be saved to the cache and the error will be returned from the GetOrSet function. Other concurrent readers will also see an error while reading.

The returned io.ReadCloser must be closed by the caller when finished.

Parameters

key: []byte
setFn: func() (CacheEntry, error)

Returns

(io.ReadCloser, error)