https://github.com/knetic/lcache
Real-through LRU in-memory cache with background refresh
https://github.com/knetic/lcache
Last synced: 7 months ago
JSON representation
Real-through LRU in-memory cache with background refresh
- Host: GitHub
- URL: https://github.com/knetic/lcache
- Owner: Knetic
- License: mit
- Created: 2020-09-01T03:10:56.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-07T18:06:07.000Z (about 5 years ago)
- Last Synced: 2023-08-15T15:37:36.556Z (about 2 years ago)
- Language: Go
- Size: 40 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
lcache
====`lcache` (L-Cache) is an in-memory read-through generic LRU cache for Go. It is heavily inspired by [Guava's LoadingCache](https://github.com/google/guava/wiki/CachesExplained), which the author has found to be one of the single most useful libraries in production use.
## How to use
To make a cache, implement and provide a `Loader`:
```go
type staticLoader struct {
data map[string]int
}func (loader *staticLoader) Load(key string) (interface{}, error) {
// in practice, this would be some data that is expensive to load
if num, exists := (*loader.data)[key]; !exists {
return nil, nil
} else {
return num, nil
}
}data := map[string]int {"some key": 42}
cache, _ := lcache.NewCache(Params{
Loader: &staticLoader{data: data},
...
})value, _ := c.Get("some key")
// prints 42
fmt.Println(value)
```## Specifics