项目作者: turboezh

项目描述 :
Priority queue (heap) based cache with 'priority' evict policy
高级语言: Go
项目地址: git://github.com/turboezh/heapcache.git
创建时间: 2017-03-09T13:21:01Z
项目社区:https://github.com/turboezh/heapcache

开源协议:MIT License

下载


heapcache

Build Status
GitHub release
Go Report Card
Maintainability
Test Coverage
Downloads
GoDoc

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:

  • 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

  1. type Foo struct {
  2. Value int
  3. Timestamp time.Time
  4. }
  5. item1 := Foo{10, time.Now()}
  6. item2 := Foo{20, time.Now().Add(time.Second)}
  7. cache := New(10, func(a, b interface{}) bool {
  8. return a.(*Foo).Timestamp.Before(b.(*Foo).Timestamp)
  9. })

Add item

  1. cache.Add("one", &item1)
  2. cache.Add("two", &item2)

Get item

  1. item, exists := cache.Get("one")
  2. if !exists {
  3. // `foo` doesn't exists in cache
  4. // `item` is nil
  5. }
  6. // cache returns `interface{}` so we need to assert type (if need so)
  7. item = item.(*Foo) // = &item1

Check item

  1. // check if cache contain all keys
  2. ok := cache.All("one", "two")
  3. // check if cache contain any of keys
  4. ok := cache.Any("one", "two")

Remove item

  1. // Remove returns false if there is no item in cache
  2. wasRemoved := cache.Remove("one")

Support on Beerpay

Hey dude! Help me out for a couple of :beers:!

Beerpay Beerpay