lambdacache is a Go package for caching data in your AWS Lambda function between sequential invocations. It stores data in the memory of the function execution context that usually spans multiple consecutive invocations.
Usage
// 1. import the package
import "github.com/udhos/lambdacache/lambdacache"
// 2. in lambda function GLOBAL context: create cache
var cache = newCache()
func newCache() *lambdacache.Cache {
options := lambdacache.Options{
Debug: true,
Retrieve: getInfo,
}
return lambdacache.New(options)
}
// 3. in lambda function HANDLER context: query cache
func HandleRequest(ctx context.Context) error {
// ...
value, errGet := cache.Get(key)
// ...
return nil
}
// getInfo retrieves key value when there is a cache miss
func getInfo(key string) (interface{}, time.Duration, error) {
const ttl = 5 * time.Minute // per-key TTL
return "put-retrieved-value-here", ttl, nil
}