https://github.com/codeation/lru
Go asynchronous LRU cache
https://github.com/codeation/lru
asynchronous go golang in-memory in-memory-caching library lru lru-cache module once sync
Last synced: 5 months ago
JSON representation
Go asynchronous LRU cache
- Host: GitHub
- URL: https://github.com/codeation/lru
- Owner: codeation
- License: mit
- Created: 2020-12-11T14:24:06.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-06-30T05:32:14.000Z (almost 2 years ago)
- Last Synced: 2025-11-22T15:16:31.318Z (7 months ago)
- Topics: asynchronous, go, golang, in-memory, in-memory-caching, library, lru, lru-cache, module, once, sync
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lru
A lru is an asynchronous LRU cache (generic version).
[](https://godoc.org/github.com/codeation/lru)
- based on golang map structure
- prevents infinite cache growth
- designed for asynchronous use
# Usage
For example, caching the output of the ioutil.ReadFile function to reduce disk I/O.
```
package main
import (
"log"
"os"
"github.com/codeation/lru"
)
func readFileContent(key string) ([]byte, error) {
log.Println("read once")
return os.ReadFile(key)
}
func main() {
cache := lru.NewSyncLRU(1024, readFileContent)
for i := 0; i < 10; i++ {
data, err := cache.Get("input.txt")
if err != nil {
log.Fatal(err)
}
log.Printf("file size is %d\n", len(data))
}
}
```
The lru.NewSyncLRU parameter is the number of cache items until the last used item is removed from the cache. The second parameter is a func to get the value for the specified key and error.
The parameter of cache.Get func is a key (filename in this case). An error is returned when the function returns an error.