---
title: GetOrSet
summary: 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.
url: https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/cache/simple/fn.GetOrSet/
---

**Kind:** Function  
**Module:** [simple](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/cache/simple/)  
**Package:** compute-sdk-go v1.11.0  
**Source:** https://github.com/fastly/compute-sdk-go/blob/d6bf71b04516ed72d6018cab6fff701a865f86e5/cache/simple/simple.go#L61

```go
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](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/cache/simple/struct.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](https://pkg.go.dev/io#ReadCloser) must be closed by the caller when
finished.

**Parameters**

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

**Returns**

`(io.ReadCloser, error)`
