gcache

package module
v0.0.0-...-ed77695 Latest Latest
Warning

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

Go to latest
Published: May 1, 2018 License: MIT Imports: 6 Imported by: 0

README

GCache2

GoDoc Go Report Card Waffle.io - Columns and their card count

Work-in-Progress: This repository will be the home of gcache2, a caching library for Golang. Ideally the only one you will ever need. To reach that ambitious goal, I am taking some time to scope and re-architecture gcache such that it becomes easier to add features, maintain existing ones, and get total testing coverage of the execution paths that matter. If you are interested in joining this effort, take a look at the issues! Stay tuned, friends. (:

Overview

The only embedded caching library you will ever need, in Golang.

Features

A variety of concurrency-safe caches are available:

  • LFU (Least-Frequently-Used)

  • LRU (Least-Recently-Used)

  • ARC (Adaptive-Replacement policy)

  • TinyLFU ("clever" admission policy)

  • RR (RandomReplacement)

Other features:

  • Expirable entries

  • Optional support for event handlers (on eviction, purge, set, loader).

  • Cache snapshots

Install

$ go get github.com/aaronwinter/gcache2

Authors

Erwan Ounn (main contributor/maintainer of gcache2)

Jun Kimura (main contributor of gcache)

Documentation

Index

Constants

View Source
const (
	TYPE_SIMPLE = "simple"
	TYPE_LRU    = "lru"
	TYPE_LFU    = "lfu"
	TYPE_ARC    = "arc"
)

Variables

View Source
var KeyNotFoundError = errors.New("Key not found.")

Functions

This section is empty.

Types

type ARC

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

Constantly balances between LRU and LFU, to improve the combined result.

func (*ARC) Debug

func (c *ARC) Debug() map[string][]int

func (*ARC) Get

func (c *ARC) Get(key interface{}) (interface{}, error)

func (*ARC) GetALL

func (c *ARC) GetALL() map[interface{}]interface{}

func (*ARC) GetIFPresent

func (c *ARC) GetIFPresent(key interface{}) (interface{}, error)

func (*ARC) Keys

func (c *ARC) Keys() []interface{}

func (*ARC) Len

func (c *ARC) Len() int

func (*ARC) Purge

func (c *ARC) Purge()

func (*ARC) Remove

func (c *ARC) Remove(key interface{}) error

func (*ARC) Set

func (c *ARC) Set(key, value interface{}) error

func (*ARC) SetWithExpire

func (c *ARC) SetWithExpire(key, value interface{}, expiration time.Duration) error

type AddedFunc

type AddedFunc func(interface{}, interface{})

type Cache

type Cache interface {
	Set(interface{}, interface{}) error
	SetWithExpire(interface{}, interface{}, time.Duration) error
	Get(interface{}) (interface{}, error)
	GetIFPresent(interface{}) (interface{}, error)
	GetALL() map[interface{}]interface{}
	Remove(interface{}) error
	Purge()
	Keys() []interface{}
	Len() int

	Debug() map[string][]int
	// contains filtered or unexported methods
}

type CacheBuilder

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

func New

func New(capacity int) *CacheBuilder

func (*CacheBuilder) ARC

func (cb *CacheBuilder) ARC() *CacheBuilder

func (*CacheBuilder) AddedFunc

func (cb *CacheBuilder) AddedFunc(addedFunc AddedFunc) *CacheBuilder

func (*CacheBuilder) Build

func (cb *CacheBuilder) Build() (Cache, error)

func (*CacheBuilder) Clock

func (cb *CacheBuilder) Clock(clock Clock) *CacheBuilder

func (*CacheBuilder) DeserializeFunc

func (cb *CacheBuilder) DeserializeFunc(deserializeFunc DeserializeFunc) *CacheBuilder

func (*CacheBuilder) EvictType

func (cb *CacheBuilder) EvictType(tp string) *CacheBuilder

func (*CacheBuilder) EvictedFunc

func (cb *CacheBuilder) EvictedFunc(evictedFunc EvictedFunc) *CacheBuilder

func (*CacheBuilder) Expiration

func (cb *CacheBuilder) Expiration(expiration time.Duration) *CacheBuilder

func (*CacheBuilder) LFU

func (cb *CacheBuilder) LFU() *CacheBuilder

func (*CacheBuilder) LRU

func (cb *CacheBuilder) LRU() *CacheBuilder

func (*CacheBuilder) LoaderExpireFunc

func (cb *CacheBuilder) LoaderExpireFunc(loaderExpireFunc LoaderExpireFunc) *CacheBuilder

Set a loader function with expiration. loaderExpireFunc: create a new value with this function if cached value is expired. If nil returned instead of time.Duration from loaderExpireFunc than value will never expire.

func (*CacheBuilder) LoaderFunc

func (cb *CacheBuilder) LoaderFunc(loaderFunc LoaderFunc) *CacheBuilder

Set a loader function. loaderFunc: create a new value with this function if cached value is expired.

func (*CacheBuilder) PurgeVisitorFunc

func (cb *CacheBuilder) PurgeVisitorFunc(purgeVisitorFunc PurgeVisitorFunc) *CacheBuilder

func (*CacheBuilder) SerializeFunc

func (cb *CacheBuilder) SerializeFunc(serializeFunc SerializeFunc) *CacheBuilder

func (*CacheBuilder) Simple

func (cb *CacheBuilder) Simple() *CacheBuilder

type Clock

type Clock interface {
	Now() time.Time
}

func NewRealClock

func NewRealClock() Clock

type DeserializeFunc

type DeserializeFunc func(interface{}, interface{}) (interface{}, error)

type EvictedFunc

type EvictedFunc func(interface{}, interface{})

type FakeClock

type FakeClock interface {
	Clock

	Advance(d time.Duration)
}

func NewFakeClock

func NewFakeClock() FakeClock

type Group

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

Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression.

func (*Group) Do

func (g *Group) Do(key interface{}, fn func() (interface{}, error), isWait bool) (interface{}, bool, error)

Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.

type LFUCache

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

func (*LFUCache) Debug

func (c *LFUCache) Debug() map[string][]int

func (*LFUCache) Get

func (c *LFUCache) Get(key interface{}) (interface{}, error)

func (*LFUCache) GetALL

func (c *LFUCache) GetALL() map[interface{}]interface{}

func (*LFUCache) GetIFPresent

func (c *LFUCache) GetIFPresent(key interface{}) (interface{}, error)

func (*LFUCache) Keys

func (c *LFUCache) Keys() []interface{}

func (*LFUCache) Len

func (c *LFUCache) Len() int

func (*LFUCache) Purge

func (c *LFUCache) Purge()

func (*LFUCache) Remove

func (c *LFUCache) Remove(key interface{}) error

func (*LFUCache) Set

func (c *LFUCache) Set(key, value interface{}) error

Set a new key-value pair

func (*LFUCache) SetWithExpire

func (c *LFUCache) SetWithExpire(key, value interface{}, expiration time.Duration) error

type LRUCache

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

func (*LRUCache) Debug

func (c *LRUCache) Debug() map[string][]int

func (*LRUCache) Get

func (c *LRUCache) Get(key interface{}) (interface{}, error)

func (*LRUCache) GetALL

func (c *LRUCache) GetALL() map[interface{}]interface{}

func (*LRUCache) GetIFPresent

func (c *LRUCache) GetIFPresent(key interface{}) (interface{}, error)

func (*LRUCache) Keys

func (c *LRUCache) Keys() []interface{}

func (*LRUCache) Len

func (c *LRUCache) Len() int

func (*LRUCache) Purge

func (c *LRUCache) Purge()

func (*LRUCache) Remove

func (c *LRUCache) Remove(key interface{}) error

func (*LRUCache) Set

func (c *LRUCache) Set(key, value interface{}) error

func (*LRUCache) SetWithExpire

func (c *LRUCache) SetWithExpire(key, value interface{}, expiration time.Duration) error

type LoaderExpireFunc

type LoaderExpireFunc func(interface{}) (interface{}, *time.Duration, error)

type LoaderFunc

type LoaderFunc func(interface{}) (interface{}, error)

type PurgeVisitorFunc

type PurgeVisitorFunc func(interface{}, interface{})

type RealClock

type RealClock struct{}

func (RealClock) Now

func (rc RealClock) Now() time.Time

type SerializeFunc

type SerializeFunc func(interface{}, interface{}) (interface{}, error)

type SimpleCache

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

func (*SimpleCache) Debug

func (c *SimpleCache) Debug() map[string][]int

func (*SimpleCache) Get

func (c *SimpleCache) Get(key interface{}) (interface{}, error)

func (*SimpleCache) GetALL

func (c *SimpleCache) GetALL() map[interface{}]interface{}

func (*SimpleCache) GetIFPresent

func (c *SimpleCache) GetIFPresent(key interface{}) (interface{}, error)

func (*SimpleCache) Keys

func (c *SimpleCache) Keys() []interface{}

func (*SimpleCache) Len

func (c *SimpleCache) Len() int

func (*SimpleCache) Purge

func (c *SimpleCache) Purge()

func (*SimpleCache) Remove

func (c *SimpleCache) Remove(key interface{}) error

func (*SimpleCache) Set

func (c *SimpleCache) Set(key, value interface{}) error

func (*SimpleCache) SetWithExpire

func (c *SimpleCache) SetWithExpire(key, value interface{}, expiration time.Duration) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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