Documentation ¶
Overview ¶
Package fastcache implements fast in-memory cache.
The package has been extracted from https://victoriametrics.com/
Index ¶
- type BigStats
- type Cache
- func (c *Cache) Del(k uint64)
- func (c *Cache) Get(dst []byte, k uint64) []byte
- func (c *Cache) GetBig(dst []byte, k uint64) (r []byte)
- func (c *Cache) GetKeys() []uint64
- func (c *Cache) Has(k uint64) bool
- func (c *Cache) HasGet(dst []byte, k uint64) ([]byte, bool)
- func (c *Cache) Reset()
- func (c *Cache) SaveToFile(filePath string) error
- func (c *Cache) SaveToFileConcurrent(filePath string, concurrency int) error
- func (c *Cache) Set(k uint64, v []byte)
- func (c *Cache) SetBig(k uint64, v []byte)
- func (c *Cache) UpdateStats(s *Stats)
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BigStats ¶
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 ¶
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 ¶
LoadFromFile loads cache data from the given filePath.
See SaveToFile* for saving cache data to file.
func LoadFromFileOrNew ¶
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 ¶
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 ¶
Del deletes value for the given k from the cache.
k contents may be modified after returning from Del.
func (*Cache) Get ¶
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 ¶
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) HasGet ¶
HasGet works identically to Get, but also returns whether the given key exists in the cache. This method makes it possible to differentiate between a stored nil/empty value versus and non-existing value.
func (*Cache) SaveToFile ¶
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 ¶
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 ¶
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 ¶
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 ¶
UpdateStats adds cache stats to s.
Call s.Reset before calling UpdateStats if s is re-used.
type Stats ¶
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 // MaxBytesSize is the maximum allowed size of the cache in bytes (aka capacity). MaxBytesSize uint64 // BigStats contains stats for GetBig/SetBig methods. BigStats }
Stats represents cache stats.
Use Cache.UpdateStats for obtaining fresh stats from the cache.