https://github.com/shaovie/ttlcache
An Efficient In-Process Object Caching Library for Managing Lifecycles
https://github.com/shaovie/ttlcache
cache go golang memcache ttl-cache
Last synced: 11 months ago
JSON representation
An Efficient In-Process Object Caching Library for Managing Lifecycles
- Host: GitHub
- URL: https://github.com/shaovie/ttlcache
- Owner: shaovie
- Created: 2023-07-13T06:47:47.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-21T08:56:13.000Z (over 2 years ago)
- Last Synced: 2025-02-12T17:21:16.248Z (about 1 year ago)
- Topics: cache, go, golang, memcache, ttl-cache
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## ttlcache
`ttlcache` is an in-process object caching library designed specifically for managing the caching and automatic release of objects with lifecycles. The cache operates on a time-based expiration mechanism, measured in seconds, and utilizes timestamp caching to avoid real-time system time retrieval. This significantly improves efficiency. Object storage employs sharding techniques to reduce concurrent access competition.
Usage:
```
import "github.com/shaovie/ttlcache"
func main() {
cache := ttlcache.New(ttlcache.BucketsCount(512),
ttlcache.BucketsMapPreAllocSize(256),
ttlcache.CleanInterval(10),
)
cache.Set("ttlcache", "nb", 1/*second*/) // The lifecycle is 1 second
val, found := cache.Get("ttlcache")
if !found {
fmt.Println("set val error")
return
}
time.Sleep(time.Millisecond * 1500)
_, found = cache.Get("ttlcache")
if !found {
fmt.Println("expired")
}
}
```
Usage Reference: `ttlcache_bench_test.go`