https://github.com/vicanso/lru-ttl
lru cache with ttl
https://github.com/vicanso/lru-ttl
lru-cache ttl-cache
Last synced: 4 months ago
JSON representation
lru cache with ttl
- Host: GitHub
- URL: https://github.com/vicanso/lru-ttl
- Owner: vicanso
- License: apache-2.0
- Created: 2020-03-18T12:15:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-04-24T02:58:06.000Z (over 3 years ago)
- Last Synced: 2025-05-18T10:07:12.473Z (5 months ago)
- Topics: lru-cache, ttl-cache
- Language: Go
- Size: 104 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lru-ttl
[](https://github.com/vicanso/lru-ttl/actions)
LRU cache with ttl. It's useful for short ttl cache.
L2Cache use lru cache as first cache, and slow cache as second cache. Lru cache should be set max entries for less memory usage but faster, slow cache is slower but larger capacity.## LRU TTL
```go
cache := lruttl.New(1000, 60 * time.Second)
cache.Add("tree.xie", "my data")
data, ok := cache.Get("tree.xie")
cache.Remove("tree.xie")
cache.Add("tree.xie", "my data", time.Second)
```## L2Cache
```go
// redisCache
l2 := lruttl.NewL2Cache(redisCache, 200, 10 * time.Minute)
ctx := context.Background()
err := l2.Set(ctx, "key", &map[string]string{
"name": "test",
})
fmt.Println(err)
m := make(map[string]string)
err = l2.Get(ctx, "key", &m)
fmt.Println(err)
fmt.Println(m)
```## Ring
```go
ringCache := lruttl.NewRing(lruttl.RingCacheParams{
Size: 10,
MaxEntries: 1000,
DefaultTTL: time.Minute,
})
lruCache := ringCache("key")
```