https://github.com/udhos/lambdacache
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.
https://github.com/udhos/lambdacache
aws aws-lambda cache go golang lambda package
Last synced: 5 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/udhos/lambdacache
- Owner: udhos
- License: mit
- Created: 2024-10-27T18:22:02.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-29T00:15:58.000Z (about 1 year ago)
- Last Synced: 2024-11-05T14:32:38.510Z (about 1 year ago)
- Topics: aws, aws-lambda, cache, go, golang, lambda, package
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/udhos/lambdacache/blob/main/LICENSE)
[](https://goreportcard.com/report/github.com/udhos/lambdacache)
[](https://pkg.go.dev/github.com/udhos/lambdacache)
# lambdacache
[lambdacache](https://github.com/udhos/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](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html) that usually spans multiple consecutive invocations.
# Benefits
- Lower load on backend services.
- Lower lambda execution time (thus lower lambda costs).
- Independent of any external cache service (e.g. redis).
- Higher robustness.
- Higher scalability.
# Usage
```golang
// 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
}
```
# Example
See:
[./cmd/lambdacache-example/main.go](./cmd/lambdacache-example/main.go)