https://github.com/qshuai/lru
An effective LRU implementation in Golang
https://github.com/qshuai/lru
effective golang lru-cache
Last synced: 5 months ago
JSON representation
An effective LRU implementation in Golang
- Host: GitHub
- URL: https://github.com/qshuai/lru
- Owner: qshuai
- Created: 2019-05-29T10:10:50.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-08-19T10:31:27.000Z (almost 4 years ago)
- Last Synced: 2024-06-20T11:48:12.263Z (about 2 years ago)
- Topics: effective, golang, lru-cache
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
lru
===
##### Usage:
```go
package main
import (
"fmt"
"github.com/qshuai/lru"
)
type user struct {
key string
score int
}
func (u *user) GetKey() interface{} {
return u.key
}
func main() {
// set capacity to 3 for this cache as a sample
cache := lru.New(3, true)
// add 4 entries, the first entry will be removed automaticlly
cache.Add(&user{"Andy", 23})
cache.Add(&user{"Tom", 24})
cache.Add(&user{"Angel", 25})
cache.Add(&user{"Nike", 26})
fmt.Println(cache.Size(), cache.Existed("Andy"), cache.Existed("Tom"),
cache.Existed("Angel"), cache.Existed("Nike"))
// access the user Tom, the tom will be move to front
cache.Get("Tom")
// now add a entry, the tail entry user named Angel will be removed
cache.Add(&user{"Jone", 27})
fmt.Println(cache.Size(), cache.Existed("Tom"), cache.Existed("Angel"),
cache.Existed("Nike"), cache.Existed("Jone"))
// remove a enry by hand, we will discard this and update cache size
cache.Remove("Jone")
fmt.Println(cache.Size(), cache.Existed("Tom"), cache.Existed("Angel"),
cache.Existed("Nike"), cache.Existed("Jone"))
}
```