heapcache

package module
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2019 License: MIT Imports: 2 Imported by: 0

README

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

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

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

Get item

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

// 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

// 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 🍻!

Beerpay Beerpay

Documentation

Overview

Example
type Foo struct {
	Value     int
	Timestamp time.Time
}

cache := New(10, func(a, b interface{}) bool {
	return a.(*Foo).Timestamp.Before(b.(*Foo).Timestamp)
})

item1 := Foo{10, time.Now()}
item2 := Foo{20, time.Now().Add(time.Second)}

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a cache abstraction. It uses user-defined comparator to evaluate priorities of cached items. Items with lowest priorities will be evicted first.

func New

func New(capacity int, cmp Less) *Cache

New creates a new Cache instance. Capacity allowed to be zero. In this case cache becomes dummy, 'Add' do nothing and items can't be stored in.

func (*Cache) Add

func (c *Cache) Add(key interface{}, value interface{})

Add adds a `value` into a cache. If `key` already exists, `value` will be overwritten. `key` must be a KeyType (see https://golang.org/ref/spec#KeyType)

func (*Cache) All

func (c *Cache) All(keys ...interface{}) bool

All checks if ALL `keys` exists

func (*Cache) Any

func (c *Cache) Any(keys ...interface{}) bool

Any checks if ANY of `keys` exists

func (*Cache) Capacity

func (c *Cache) Capacity() int

Capacity returns capacity of cache

func (*Cache) ChangeCapacity

func (c *Cache) ChangeCapacity(delta int)

ChangeCapacity change cache capacity by `delta`. If `delta` is positive cache capacity will be expanded, if `delta` is negative, it will be shrunk. Redundant items will be evicted.

func (*Cache) Evict

func (c *Cache) Evict(count int) int

Evict removes `count` elements with lowest priority.

func (*Cache) Fix

func (c *Cache) Fix()

Fix calls heap.Init to reorder heap.

func (*Cache) Get

func (c *Cache) Get(key interface{}) (interface{}, bool)

Get gets a value by `key`

func (*Cache) Len

func (c *Cache) Len() int

Len returns a number of items in cache

func (*Cache) Purge

func (c *Cache) Purge()

Purge removes all items

func (*Cache) Remove

func (c *Cache) Remove(keys ...interface{}) (removed int)

Remove removes values by keys Returns number of actually removed items

func (*Cache) SetCapacity

func (c *Cache) SetCapacity(capacity int)

SetCapacity sets cache capacity. Redundant items will be evicted. Capacity never become less than zero.

type Less

type Less func(a, b interface{}) bool

Less is a user-defined function that compares items to evaluate their priorities. Must return true if `a < b`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL