Priority queue (heap) based cache with 'priority' evict policy
This cache implementation is based on priority queue (see Heap).
It uses user-defined comparator to evaluate priorities of cached items. Items with lowest priorities will be evicted first.
Features:
Go >= 1.11
https://godoc.org/github.com/turboezh/heapcache
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)
})
cache.Add("one", &item1)
cache.Add("two", &item2)
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 if cache contain all keys
ok := cache.All("one", "two")
// check if cache contain any of keys
ok := cache.Any("one", "two")
// Remove returns false if there is no item in cache
wasRemoved := cache.Remove("one")
Hey dude! Help me out for a couple of !