Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 ¶ added in v1.1.0
LoadFromFile loads cache data from the given filePath.
See SaveToFile* for saving cache data to file.
func LoadFromFileOrNew ¶ added in v1.1.0
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.
k contents may be modified after returning from Get.
func (*Cache) SaveToFile ¶ added in v1.1.0
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 ¶ added in v1.3.0
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.
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.
k and v contents may be modified after returning from Set.
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. Collisions 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 }
Stats represents cache stats.
Use Cache.UpdateStats for obtaining fresh stats from the cache.