Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cf3b7S/go-lru
golang fast concurrent lru cache with thread safe
https://github.com/cf3b7S/go-lru
Last synced: 3 months ago
JSON representation
golang fast concurrent lru cache with thread safe
- Host: GitHub
- URL: https://github.com/cf3b7S/go-lru
- Owner: cf3b7S
- Created: 2016-08-25T08:21:52.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2016-12-09T07:26:43.000Z (almost 8 years ago)
- Last Synced: 2024-04-14T01:50:12.027Z (7 months ago)
- Language: Go
- Homepage: https://github.com/khowarizmi/go-lru
- Size: 12.7 KB
- Stars: 15
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-lru
golang fast concurrent lru cache with thread safe.### Installation
`go get github.com/khowarizmi/go-lru`
### Document
[doc](https://godoc.org/github.com/khowarizmi/go-lru)### Usage
```
package mainimport (
"fmt"
"github.com/khowarizmi/go-lru"
)func inc(entry *lru.Entry) {
entry.Value = entry.Value.(int) + 1
}func callback(entry *lru.Entry) {
fmt.Println("Remove", entry.Key, entry.Value.(int))
}func main() {
// create a lru with 960 entries
cache := lru.New(960)// Set key1 with int 1 to the cache
cache.Set("key1", 1)// Set key2 with string "value" to the cache
cache.Set("key2", "value")// Get the key1 from cache and convert to int
key1, found := cache.Get("key1")
if found {
fmt.Println("found key1", key1.(int))
}// Update key1 with func inc
cache.Update("key1", inc)// Set evict callback
cache.OnRemove = callback
cache.Remove("key1")
}
```### Test
`go test .`
### BenchMark
`go test -bench . -benchtime 10s`