A light-weight caching library for Go. It can cache the results of a function with given TTL.
- Return value of the function to be cached should be exactly a result (of any type) and an error
- Function to be cached should not be declared using
var fn = func
syntax butfunc fn
instead. (This is because the library uses reflection to fetch the function name) - Data in the
InApp
store just expires, but is never deleted even after TTL. So it should be used responsibly.
- Init config (only to be done once, generally at the start of your service)
//call Init with config map (refer config.json for sample)
Init(cfg)
- Instantiate a cache store (store is meant to be reused across calls to Cache)
//store is to be instantiated only once and not on every call to Cache()
store := NewInApp()
- Use the store in Cache function as
res, err := Cache(ctx, key, store, fn, args...)
- Then you need to type case the result into the expected return type of the function
s, _ := res.(string)
Use any of the available stores like -
InApp,
InRedis,
or implement your own custom store.
see examples_test.go
for more