fastcache: github.com/VictoriaMetrics/fastcache Index | Files

package fastcache

import "github.com/VictoriaMetrics/fastcache"

Index

Package Files

bigcache.go fastcache.go file.go malloc_mmap.go

type BigStats Uses

type BigStats struct {
    // GetBigCalls is the number of GetBig calls.
    GetBigCalls uint64

    // SetBigCalls is the number of SetBig calls.
    SetBigCalls uint64

    // TooBigKeyErrors is the number of calls to SetBig with too big key.
    TooBigKeyErrors uint64

    // InvalidMetavalueErrors is the number of calls to GetBig resulting
    // to invalid metavalue.
    InvalidMetavalueErrors uint64

    // InvalidValueLenErrors is the number of calls to GetBig resulting
    // to a chunk with invalid length.
    InvalidValueLenErrors uint64

    // InvalidValueHashErrors is the number of calls to GetBig resulting
    // to a chunk with invalid hash value.
    InvalidValueHashErrors uint64
}

BigStats contains stats for GetBig/SetBig methods.

type Cache Uses

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

Cache is a fast thread-safe inmemory cache optimized for big number of entries.

It has much lower impact on GC comparing to a simple `map[string][]byte`.

Use New or LoadFromFile* for creating new cache instance. Concurrent goroutines may call any Cache methods on the same cache instance.

Call Reset when the cache is no longer needed. This reclaims the allocated memory.

func LoadFromFile Uses

func LoadFromFile(filePath string) (*Cache, error)

LoadFromFile loads cache data from the given filePath.

See SaveToFile* for saving cache data to file.

func LoadFromFileOrNew Uses

func LoadFromFileOrNew(filePath string, maxBytes int) *Cache

LoadFromFileOrNew tries loading cache data from the given filePath.

The function falls back to creating new cache with the given maxBytes capacity if error occurs during loading the cache from file.

func New Uses

func New(maxBytes int) *Cache

New returns new cache with the given maxBytes capacity in bytes.

maxBytes must be smaller than the available RAM size for the app, since the cache holds data in memory.

If maxBytes is less than 32MB, then the minimum cache capacity is 32MB.

func (*Cache) Del Uses

func (c *Cache) Del(k []byte)

Del deletes value for the given k from the cache.

k contents may be modified after returning from Del.

func (*Cache) Get Uses

func (c *Cache) Get(dst, k []byte) []byte

Get appends value by the key k to dst and returns the result.

Get allocates new byte slice for the returned value if dst is nil.

Get returns only values stored in c via Set.

k contents may be modified after returning from Get.

func (*Cache) GetBig Uses

func (c *Cache) GetBig(dst, k []byte) []byte

GetBig searches for the value for the given k, appends it to dst and returns the result.

GetBig returns only values stored via SetBig. It doesn't work with values stored via other methods.

k contents may be modified after returning from GetBig.

func (*Cache) Reset Uses

func (c *Cache) Reset()

Reset removes all the items from the cache.

func (*Cache) SaveToFile Uses

func (c *Cache) SaveToFile(filePath string) error

SaveToFile atomically saves cache data to the given filePath using a single CPU core.

SaveToFile may be called concurrently with other operations on the cache.

The saved data may be loaded with LoadFromFile*.

See also SaveToFileConcurrent for faster saving to file.

func (*Cache) SaveToFileConcurrent Uses

func (c *Cache) SaveToFileConcurrent(filePath string, concurrency int) error

SaveToFileConcurrent saves cache data to the given filePath using concurrency CPU cores.

SaveToFileConcurrent may be called concurrently with other operations on the cache.

The saved data may be loaded with LoadFromFile*.

See also SaveToFile.

func (*Cache) Set Uses

func (c *Cache) Set(k, v []byte)

Set stores (k, v) in the cache.

Get must be used for reading the stored entry.

The stored entry may be evicted at any time either due to cache overflow or due to unlikely hash collision. Pass higher maxBytes value to New if the added items disappear frequently.

(k, v) entries with summary size exceeding 64KB aren't stored in the cache. SetBig can be used for storing entries exceeding 64KB.

k and v contents may be modified after returning from Set.

func (*Cache) SetBig Uses

func (c *Cache) SetBig(k, v []byte)

SetBig sets (k, v) to c where len(v) may exceed 64KB.

GetBig must be used for reading stored values.

The stored entry may be evicted at any time either due to cache overflow or due to unlikely hash collision. Pass higher maxBytes value to New if the added items disappear frequently.

It is safe to store entries smaller than 64KB with SetBig.

k and v contents may be modified after returning from SetBig.

func (*Cache) UpdateStats Uses

func (c *Cache) UpdateStats(s *Stats)

UpdateStats adds cache stats to s.

Call s.Reset before calling UpdateStats if s is re-used.

type Stats Uses

type Stats struct {
    // GetCalls is the number of Get calls.
    GetCalls uint64

    // SetCalls is the number of Set calls.
    SetCalls uint64

    // Misses is the number of cache misses.
    Misses uint64

    // Collisions is the number of cache collisions.
    //
    // Usually the number of collisions must be close to zero.
    // High number of collisions suggest something wrong with cache.
    Collisions uint64

    // Corruptions is the number of detected corruptions of the cache.
    //
    // Corruptions may occur when corrupted cache is loaded from file.
    Corruptions uint64

    // EntriesCount is the current number of entries in the cache.
    EntriesCount uint64

    // BytesSize is the current size of the cache in bytes.
    BytesSize uint64

    // BigStats contains stats for GetBig/SetBig methods.
    BigStats
}

Stats represents cache stats.

Use Cache.UpdateStats for obtaining fresh stats from the cache.

func (*Stats) Reset Uses

func (s *Stats) Reset()

Reset resets s, so it may be re-used again in Cache.UpdateStats.

Package fastcache imports 14 packages (graph) and is imported by 3 packages. Updated 2019-03-20. Refresh now. Tools for package owners.