https://github.com/praharshjain/go-cache
A caching library for Go
https://github.com/praharshjain/go-cache
cache caching caching-library go go-lib go-library go-package golang golang-lib golang-library golang-package
Last synced: 3 months ago
JSON representation
A caching library for Go
- Host: GitHub
- URL: https://github.com/praharshjain/go-cache
- Owner: praharshjain
- License: mit
- Created: 2022-10-23T19:51:02.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-11T05:29:31.000Z (about 3 years ago)
- Last Synced: 2025-12-18T05:34:11.669Z (6 months ago)
- Topics: cache, caching, caching-library, go, go-lib, go-library, go-package, golang, golang-lib, golang-library, golang-package
- Language: Go
- Homepage:
- Size: 110 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
go-cache
==========
A light-weight caching library for Go. It can cache the results of a function with given TTL.
Known caveats
----------------
1. Return value of the function to be cached should be exactly a result (of any type) and an error
2. Function to be cached should not be declared using `var fn = func` syntax but `func fn` instead. (This is because the library uses reflection to fetch the function name)
3. Data in the `InApp` store just expires, but is never deleted even after TTL. So it should be used responsibly.
Usage
----------------
1. Init config (only to be done once, generally at the start of your service)
```go
//call Init with config map (refer config.json for sample)
Init(cfg)
````
2. Instantiate a cache store (store is meant to be reused across calls to Cache)
```go
//store is to be instantiated only once and not on every call to Cache()
store := NewInApp()
```
3. Use the store in Cache function as
```go
res, err := Cache(ctx, key, store, fn, args...)
```
4. Then you need to type case the result into the expected return type of the function
```go
s, _ := res.(string)
```
Use any of the available stores like -
```
InApp,
InRedis,
or implement your own custom store.
```
see `examples_test.go` for more