https://github.com/turboezh/heapcache
Priority queue (heap) based cache with 'priority' evict policy
https://github.com/turboezh/heapcache
cache heap priority priority-queue
Last synced: 2 months ago
JSON representation
Priority queue (heap) based cache with 'priority' evict policy
- Host: GitHub
- URL: https://github.com/turboezh/heapcache
- Owner: turboezh
- License: mit
- Created: 2017-03-09T13:21:01.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-02-06T15:17:56.000Z (about 7 years ago)
- Last Synced: 2023-07-11T19:10:40.991Z (over 2 years ago)
- Topics: cache, heap, priority, priority-queue
- Language: Go
- Homepage:
- Size: 36.1 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# heapcache
[](https://travis-ci.org/turboezh/heapcache)
[](https://github.com/turboezh/heapcache/releases)
[](https://goreportcard.com/report/github.com/turboezh/heapcache)
[](https://codeclimate.com/github/turboezh/heapcache/maintainability)
[](https://codeclimate.com/github/turboezh/heapcache/test_coverage)

[](https://godoc.org/github.com/turboezh/heapcache)
This cache implementation is based on priority queue (see [Heap](https://golang.org/pkg/container/heap/)).
It uses user-defined comparator to evaluate priorities of cached items. Items with lowest priorities will be evicted first.
Features:
- simple standard data structure;
- no write locks on get operations;
- capacity may be changed at any time.
# Requirements
Go >= 1.11
# Documentation
https://godoc.org/github.com/turboezh/heapcache
# Examples
## Cache
```go
type Foo struct {
Value int
Timestamp time.Time
}
item1 := Foo{10, time.Now()}
item2 := Foo{20, time.Now().Add(time.Second)}
cache := New(10, func(a, b interface{}) bool {
return a.(*Foo).Timestamp.Before(b.(*Foo).Timestamp)
})
```
## Add item
```go
cache.Add("one", &item1)
cache.Add("two", &item2)
```
## Get item
```go
item, exists := cache.Get("one")
if !exists {
// `foo` doesn't exists in cache
// `item` is nil
}
// cache returns `interface{}` so we need to assert type (if need so)
item = item.(*Foo) // = &item1
```
## Check item
```go
// check if cache contain all keys
ok := cache.All("one", "two")
// check if cache contain any of keys
ok := cache.Any("one", "two")
```
## Remove item
```go
// Remove returns false if there is no item in cache
wasRemoved := cache.Remove("one")
```
## Support on Beerpay
Hey dude! Help me out for a couple of :beers:!
[](https://beerpay.io/turboezh/heapcache) [](https://beerpay.io/turboezh/heapcache?focus=wish)