Use c.Set(key string, value interface{}, expiration time.Duration) to store items and c.Get(key string) (interface{}, bool) to retrieve them.
Expiration Options:
cache.DefaultExpiration: Uses the default expiration time defined when the cache was created.cache.NoExpiration: The item will not expire until it is manually deleted or overwritten.
Note on Type Assertion:
Because c.Get returns an interface{}, you must use Go type assertion to convert the value back to its original type (e.g., foo.(string) or x.(*MyStruct)).
import (
"fmt"
"github.com/patrickmn/go-cache"
"time"
)
func main() {
c := cache.New(5*time.Minute, 10*time.Minute)
// Set with default expiration
c.Set("foo", "bar", cache.DefaultExpiration)
// Set with no expiration
c.Set("baz", 42, cache.NoExpiration)
// Get value with type assertion
foo, found := c.Get("foo")
if found {
fmt.Println(foo.(string))
}
}