bigcache

package
v0.3.2-0...-638bef3 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2018 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const ErrCannotRetrieveEntry = iteratorError("Could not retrieve entry from cache")

ErrCannotRetrieveEntry is reported when entry cannot be retrieved from underlying

View Source
const ErrInvalidIteratorState = iteratorError("Iterator is in invalid state. Use SetNext() to move to next position")

ErrInvalidIteratorState is reported when iterator is in invalid state

View Source
const NO_EXPIRY uint64 = 0

NO_EXPIRY means data placed into a shard will never expire

Variables

This section is empty.

Functions

func DefaultLogger

func DefaultLogger() *log.Logger

DefaultLogger returns a `Logger` implementation backed by stdlib's log

Types

type BigCache

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

BigCache is fast, concurrent, evicting cache created to keep big number of entries without impact on performance. It keeps entries on heap but omits GC for them. To achieve that operations on bytes arrays take place, therefore entries (de)serialization in front of the cache will be needed in most use cases.

func NewBigCache

func NewBigCache(config Config) (*BigCache, error)

NewBigCache initialize new instance of BigCache

func (*BigCache) Delete

func (c *BigCache) Delete(key string) error

Delete removes the key

func (*BigCache) Get

func (c *BigCache) Get(key string) ([]byte, error)

Get reads entry for the key

func (*BigCache) Iterator

func (c *BigCache) Iterator() *EntryInfoIterator

Iterator returns iterator function to iterate over EntryInfo's from whole cache.

func (*BigCache) Len

func (c *BigCache) Len() int

Len computes number of entries in cache

func (*BigCache) Reset

func (c *BigCache) Reset() error

Reset empties all cache shards

func (*BigCache) Set

func (c *BigCache) Set(key string, entry []byte, duration time.Duration) (uint64, error)

Set saves entry under the key

func (*BigCache) Stats

func (c *BigCache) Stats() Stats

Stats returns cache's statistics

type Config

type Config struct {
	// Number of cache shards, value must be a power of two
	Shards int
	// Time after which entry can be evicted
	LifeWindow time.Duration
	// Interval between removing expired entries (clean up).
	// If set to <= 0 then no action is performed. Setting to < 1 second is counterproductive — bigcache has a one second resolution.
	CleanWindow time.Duration
	// Max number of entries in life window. Used only to calculate initial size for cache shards.
	// When proper value is set then additional memory allocation does not occur.
	MaxEntriesInWindow int
	// Max size of entry in bytes. Used only to calculate initial size for cache shards.
	MaxEntrySize int
	// Verbose mode prints information about new memory allocation
	Verbose bool
	// Hasher used to map between string keys and unsigned 64bit integers, by default fnv64 hashing is used.
	Hasher Hasher
	// HardMaxCacheSize is a limit for cache size in MB. Cache will not allocate more memory than this limit.
	// It can protect application from consuming all available memory on machine, therefore from running OOM Killer.
	// Default value is 0 which means unlimited size. When the limit is higher than 0 and reached then
	// the oldest entries are overridden for the new ones.
	HardMaxCacheSize int
	// OnRemove is a callback fired when the oldest entry is removed because of its expiration time or no space left
	// for the new entry. Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
	OnRemove func(key string, entry []byte)

	// Logger is a logging interface and used in combination with `Verbose`
	// Defaults to `DefaultLogger()`
	Logger Logger
}

Config for BigCache

func DefaultConfig

func DefaultConfig() Config

DefaultConfig initializes config with default values. When load for BigCache can be predicted in advance then it is better to use custom config.

func (*Config) SetShard

func (c *Config) SetShard(num int)

SetShard sets the number of shards

type EntryInfo

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

EntryInfo holds informations about entry in the cache

func (EntryInfo) Hash

func (e EntryInfo) Hash() uint64

Hash returns entry's hash value

func (EntryInfo) Key

func (e EntryInfo) Key() string

Key returns entry's underlying key

func (EntryInfo) Timestamp

func (e EntryInfo) Timestamp() uint64

Timestamp returns entry's timestamp (time of insertion)

func (EntryInfo) Value

func (e EntryInfo) Value() []byte

Value returns entry's underlying value

type EntryInfoIterator

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

EntryInfoIterator allows to iterate over entries in the cache

func (*EntryInfoIterator) SetNext

func (it *EntryInfoIterator) SetNext() bool

SetNext moves to next element and returns true if it exists.

func (*EntryInfoIterator) Value

func (it *EntryInfoIterator) Value() (EntryInfo, error)

Value returns current value from the iterator

type EntryNotFoundError

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

EntryNotFoundError is an error type struct which is returned when entry was not found for provided key

func (EntryNotFoundError) Error

func (e EntryNotFoundError) Error() string

Error returned when entry does not exist.

type Hasher

type Hasher interface {
	Sum64(string) uint64
}

Hasher is responsible for generating unsigned, 64 bit hash of provided string. Hasher should minimize collisions (generating same hash for different strings) and while performance is also important fast functions are preferable (i.e. you can use FarmHash family).

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
}

Logger is invoked when `Config.Verbose=true`

type Stats

type Stats struct {
	// Hits is a number of successfully found keys
	Hits int64
	// Misses is a number of not found keys
	Misses int64
	// DelHits is a number of successfully deleted keys
	DelHits int64
	// DelMisses is a number of not deleted keys
	DelMisses int64
	// Collisions is a number of happened key-collisions
	Collisions int64

	EvictCount int64
}

Stats stores cache statistics

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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