https://github.com/rutaka-n/plrucache
(p)LRU in memory cache
https://github.com/rutaka-n/plrucache
cache lru-algorithm lru-cache memory
Last synced: 3 months ago
JSON representation
(p)LRU in memory cache
- Host: GitHub
- URL: https://github.com/rutaka-n/plrucache
- Owner: rutaka-n
- License: mit
- Created: 2023-12-17T18:26:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-01T18:10:34.000Z (about 1 year ago)
- Last Synced: 2025-01-27T06:32:18.227Z (5 months ago)
- Topics: cache, lru-algorithm, lru-cache, memory
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/rutaka-n/plrucache)
# pLRUCache
(pseudo) LRU cache is a library that implements in-memory (p)LRU cache with focus on efficient memory utilization.
It uses static queues to track expiration time and least recently used items in preallocated memory, so it has minimal
memory footprint. This approach helps to avoid high memory consumtion on the peak load.
It is pseudo LRU, since it relies on `time.Time` to identify least recently used items, so in theory it might drop
not actually least recently used item, but almost least recently used one.
It uses `sync.Mutex` to deal with concurrent read/write operations.## Install
```sh
go get github.com/rutaka-n/plrucache
```
## Usage
```sh
package mainimport (
"fmt"
lru "github.com/rutaka-n/plrucache"
"time"
)type item struct {
id int64
val string
}func main() {
cacheSize := 128
expirationTime := 300 * time.Second
cache := lru.New[string, item](cacheSize, expirationTime)key := "k1"
value := item{1, "hello, world"}
cache.Set(key, value)res, ok := cache.Get(key)
if !ok {
panic("item is not in cache")
}
fmt.Printf("%+v", res)
}
```