cache.v1: maze.io/x/cache.v1 Index | Files

package cache

import "maze.io/x/cache.v1"

Package cache contains caching functions

Index

Package Files

backend.go cache.go gc.go hash.go kv.go lru.go

Variables

var Hasher func() hash.Hash

Hasher is the hash interface used for computing hashes by the Hash function. By default an FNV-1a hash function is used.

func Hash Uses

func Hash(value interface{}) ([]byte, error)

Hash an arbitrary object.

func HashUsing Uses

func HashUsing(value interface{}, hasher hash.Hash) ([]byte, error)

HashUsing hashes an arbitrary object using the provider hasher.

type Backend Uses

type Backend interface {
    // Len returns the number of unique keys in the cache
    Len() int

    // Contains checks if a key exists in the cache
    Contains(key interface{}) (bool, error)

    // Delete an item from the cache
    Delete(key interface{}) (bool, error)

    // Get an item from the cache
    Get(key interface{}) (interface{}, bool, error)

    // Iter iterates over all the key-value pairs in the cache
    Iter(func(key, value interface{})) error

    // Set an item in the cache
    Set(key, value interface{}) error

    // Replace an item in the cache if it exists, ignore otherwise
    Replace(key, value interface{}) (bool, error)
}

Backend defines an interface for storing cached items.

type Cache Uses

type Cache interface {
    // Len returns the number of unique keys in the cache
    Len() int

    // Contains if the item is found in the cache
    Contains(key interface{}) (bool, error)

    // Get an item from the cache
    Get(key interface{}) (interface{}, bool, error)

    // Set an item in the cache
    Set(key, value interface{}) error

    // RunGC runs garbage collection on the cache
    RunGC()
}

Cache describes an interface for storing arbitrary key/value pairs. Structs implementing the Cache interface should have a configurable Backend.

type DiskKV Uses

type DiskKV struct {
    Hasher hash.Hash
    // contains filtered or unexported fields
}

DiskKV implements a concurrent safe on-disk key-value store.

func NewDiskKV Uses

func NewDiskKV(path string, perm os.FileMode) (*DiskKV, error)

func (*DiskKV) Contains Uses

func (d *DiskKV) Contains(key interface{}) (bool, error)

func (*DiskKV) Delete Uses

func (d *DiskKV) Delete(key interface{}) (bool, error)

func (*DiskKV) Get Uses

func (d *DiskKV) Get(key interface{}) (interface{}, bool, error)

func (*DiskKV) Iter Uses

func (d *DiskKV) Iter(fn func(key, value interface{})) error

func (*DiskKV) Len Uses

func (d *DiskKV) Len() int

func (*DiskKV) Replace Uses

func (d *DiskKV) Replace(key, value interface{}) (bool, error)

func (*DiskKV) Set Uses

func (d *DiskKV) Set(key, value interface{}) error

type DiskLRU Uses

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

func NewDiskLRU Uses

func NewDiskLRU(path string, perm os.FileMode, size uint) (d *DiskLRU, err error)

func (*DiskLRU) Contains Uses

func (d *DiskLRU) Contains(key interface{}) (ok bool, err error)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*DiskLRU) Delete Uses

func (d *DiskLRU) Delete(key interface{}) (bool, error)

func (*DiskLRU) Get Uses

func (d *DiskLRU) Get(key interface{}) (interface{}, bool, error)

func (*DiskLRU) Iter Uses

func (d *DiskLRU) Iter(fn func(key, value interface{})) error

func (*DiskLRU) Len Uses

func (d *DiskLRU) Len() int

func (*DiskLRU) Replace Uses

func (d *DiskLRU) Replace(key, value interface{}) (bool, error)

func (*DiskLRU) Set Uses

func (d *DiskLRU) Set(key, value interface{}) error

Set an item in the cache.

type GC Uses

type GC struct {
    Interval time.Duration
    Cache    Cache
    // contains filtered or unexported fields
}

func NewGC Uses

func NewGC(interval time.Duration, cache Cache) *GC

func (*GC) Run Uses

func (gc *GC) Run()

func (*GC) Stop Uses

func (gc *GC) Stop()

type MemoryKV Uses

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

MemoryKV implements a concurrent safe in-memory key-value store.

func NewMemoryKV Uses

func NewMemoryKV() *MemoryKV

func (*MemoryKV) Contains Uses

func (kv *MemoryKV) Contains(key interface{}) (bool, error)

func (*MemoryKV) Delete Uses

func (kv *MemoryKV) Delete(key interface{}) (bool, error)

func (*MemoryKV) Get Uses

func (kv *MemoryKV) Get(key interface{}) (interface{}, bool, error)

func (*MemoryKV) Iter Uses

func (kv *MemoryKV) Iter(fn func(key, value interface{})) error

func (*MemoryKV) Len Uses

func (kv *MemoryKV) Len() int

func (*MemoryKV) Replace Uses

func (kv *MemoryKV) Replace(key, value interface{}) (bool, error)

func (*MemoryKV) Set Uses

func (kv *MemoryKV) Set(key, value interface{}) error

type MemoryLRU Uses

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

MemoryLRU implements a concurrent safe in-memory LRU key-value store.

func NewMemoryLRU Uses

func NewMemoryLRU(size uint) *MemoryLRU

func (*MemoryLRU) Contains Uses

func (m *MemoryLRU) Contains(key interface{}) (ok bool, err error)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*MemoryLRU) Delete Uses

func (m *MemoryLRU) Delete(key interface{}) (bool, error)

func (*MemoryLRU) Get Uses

func (m *MemoryLRU) Get(key interface{}) (interface{}, bool, error)

Get an item from the cache.

func (*MemoryLRU) Iter Uses

func (m *MemoryLRU) Iter(fn func(key, value interface{})) error

Iter iterates over all key-value pairs, without updating the recent-ness or deleting items for being stale.

func (*MemoryLRU) Len Uses

func (m *MemoryLRU) Len() int

Len returns the number of items in the cache.

func (*MemoryLRU) Replace Uses

func (m *MemoryLRU) Replace(key, value interface{}) (bool, error)

Replace an item in the cache.

func (*MemoryLRU) Set Uses

func (m *MemoryLRU) Set(key, value interface{}) error

Set an item in the cache.

type SimpleCache Uses

type SimpleCache struct {
    Backend
}

SimpleCache is a cache without any additional features.

func NewCache Uses

func NewCache() *SimpleCache

NewCache returns a simple cache without expiration. By default the MemoryKV backend is used.

func (*SimpleCache) RunGC Uses

func (c *SimpleCache) RunGC()

type TTLCache Uses

type TTLCache struct {
    Backend Backend
    TTL     time.Duration
    // contains filtered or unexported fields
}

TTLCache is a cache with values that can expire.

func NewTTLCache Uses

func NewTTLCache(ttl, purge time.Duration) *TTLCache

NewTTLCache runs with a timed garbage collector that runs at the selected interval. By default the MemoryKV backend is used.

func (*TTLCache) Contains Uses

func (c *TTLCache) Contains(key interface{}) (bool, error)

Contains checks if a key is contained in the cache and if it hasn't expired.

func (*TTLCache) Get Uses

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

Get a value from the cache.

func (*TTLCache) Len Uses

func (c *TTLCache) Len() int

Len is the number of cached entries, it does not check if the keys are expired.

func (*TTLCache) RunGC Uses

func (c *TTLCache) RunGC()

RunGC removes expired entries from the cache.

func (*TTLCache) Set Uses

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

Set a key-value pair.

func (*TTLCache) Stop Uses

func (c *TTLCache) Stop()

Stop the GC process.

type TTLCacheItem Uses

type TTLCacheItem struct {
    Value  interface{}
    Expire int64
}

TTLCacheItem is a single cached item.

Package cache imports 12 packages (graph). Updated 2019-02-22. Refresh now. Tools for package owners.