2018-12-19 10:02:07 +00:00
|
|
|
package bigcache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minimumEntriesInShard = 10 // Minimum number of entries in single shard
|
|
|
|
)
|
|
|
|
|
|
|
|
// BigCache is fast, concurrent, evicting cache created to keep big number of entries without impact on performance.
|
2019-11-04 10:08:22 +00:00
|
|
|
// It keeps entries on heap but omits GC for them. To achieve that, operations take place on byte arrays,
|
2018-12-19 10:02:07 +00:00
|
|
|
// therefore entries (de)serialization in front of the cache will be needed in most use cases.
|
|
|
|
type BigCache struct {
|
|
|
|
shards []*cacheShard
|
|
|
|
lifeWindow uint64
|
|
|
|
clock clock
|
|
|
|
hash Hasher
|
|
|
|
config Config
|
|
|
|
shardMask uint64
|
|
|
|
maxShardSize uint32
|
2019-11-04 10:08:22 +00:00
|
|
|
close chan struct{}
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
// RemoveReason is a value used to signal to the user why a particular key was removed in the OnRemove callback.
|
|
|
|
type RemoveReason uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Expired means the key is past its LifeWindow.
|
|
|
|
Expired RemoveReason = iota
|
|
|
|
// NoSpace means the key is the oldest and the cache size was at its maximum when Set was called, or the
|
|
|
|
// entry exceeded the maximum shard size.
|
|
|
|
NoSpace
|
|
|
|
// Deleted means Delete was called and this key was removed as a result.
|
|
|
|
Deleted
|
|
|
|
)
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// NewBigCache initialize new instance of BigCache
|
|
|
|
func NewBigCache(config Config) (*BigCache, error) {
|
|
|
|
return newBigCache(config, &systemClock{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBigCache(config Config, clock clock) (*BigCache, error) {
|
|
|
|
|
|
|
|
if !isPowerOfTwo(config.Shards) {
|
|
|
|
return nil, fmt.Errorf("Shards number must be power of two")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Hasher == nil {
|
|
|
|
config.Hasher = newDefaultHasher()
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := &BigCache{
|
|
|
|
shards: make([]*cacheShard, config.Shards),
|
|
|
|
lifeWindow: uint64(config.LifeWindow.Seconds()),
|
|
|
|
clock: clock,
|
|
|
|
hash: config.Hasher,
|
|
|
|
config: config,
|
|
|
|
shardMask: uint64(config.Shards - 1),
|
|
|
|
maxShardSize: uint32(config.maximumShardSize()),
|
2019-11-04 10:08:22 +00:00
|
|
|
close: make(chan struct{}),
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
var onRemove func(wrappedEntry []byte, reason RemoveReason)
|
|
|
|
if config.OnRemove != nil {
|
2019-10-09 14:22:53 +00:00
|
|
|
onRemove = cache.providedOnRemove
|
2019-11-04 10:08:22 +00:00
|
|
|
} else if config.OnRemoveWithReason != nil {
|
|
|
|
onRemove = cache.providedOnRemoveWithReason
|
|
|
|
} else {
|
|
|
|
onRemove = cache.notProvidedOnRemove
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < config.Shards; i++ {
|
|
|
|
cache.shards[i] = initNewShard(config, onRemove, clock)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.CleanWindow > 0 {
|
|
|
|
go func() {
|
2019-11-04 10:08:22 +00:00
|
|
|
ticker := time.NewTicker(config.CleanWindow)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case t := <-ticker.C:
|
|
|
|
cache.cleanUp(uint64(t.Unix()))
|
|
|
|
case <-cache.close:
|
|
|
|
return
|
|
|
|
}
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache, nil
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
// Close is used to signal a shutdown of the cache when you are done with it.
|
|
|
|
// This allows the cleaning goroutines to exit and ensures references are not
|
|
|
|
// kept to the cache preventing GC of the entire cache.
|
|
|
|
func (c *BigCache) Close() error {
|
|
|
|
close(c.close)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// Get reads entry for the key.
|
2019-11-04 10:08:22 +00:00
|
|
|
// It returns an ErrEntryNotFound when
|
2018-12-19 10:02:07 +00:00
|
|
|
// no entry exists for the given key.
|
|
|
|
func (c *BigCache) Get(key string) ([]byte, error) {
|
|
|
|
hashedKey := c.hash.Sum64(key)
|
|
|
|
shard := c.getShard(hashedKey)
|
|
|
|
return shard.get(key, hashedKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set saves entry under the key
|
|
|
|
func (c *BigCache) Set(key string, entry []byte) error {
|
|
|
|
hashedKey := c.hash.Sum64(key)
|
|
|
|
shard := c.getShard(hashedKey)
|
|
|
|
return shard.set(key, hashedKey, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the key
|
|
|
|
func (c *BigCache) Delete(key string) error {
|
|
|
|
hashedKey := c.hash.Sum64(key)
|
|
|
|
shard := c.getShard(hashedKey)
|
|
|
|
return shard.del(key, hashedKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset empties all cache shards
|
|
|
|
func (c *BigCache) Reset() error {
|
|
|
|
for _, shard := range c.shards {
|
|
|
|
shard.reset(c.config)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len computes number of entries in cache
|
|
|
|
func (c *BigCache) Len() int {
|
|
|
|
var len int
|
|
|
|
for _, shard := range c.shards {
|
|
|
|
len += shard.len()
|
|
|
|
}
|
|
|
|
return len
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
// Capacity returns amount of bytes store in the cache.
|
|
|
|
func (c *BigCache) Capacity() int {
|
|
|
|
var len int
|
|
|
|
for _, shard := range c.shards {
|
|
|
|
len += shard.capacity()
|
|
|
|
}
|
|
|
|
return len
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// Stats returns cache's statistics
|
|
|
|
func (c *BigCache) Stats() Stats {
|
|
|
|
var s Stats
|
|
|
|
for _, shard := range c.shards {
|
|
|
|
tmp := shard.getStats()
|
|
|
|
s.Hits += tmp.Hits
|
|
|
|
s.Misses += tmp.Misses
|
|
|
|
s.DelHits += tmp.DelHits
|
|
|
|
s.DelMisses += tmp.DelMisses
|
|
|
|
s.Collisions += tmp.Collisions
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator returns iterator function to iterate over EntryInfo's from whole cache.
|
|
|
|
func (c *BigCache) Iterator() *EntryInfoIterator {
|
|
|
|
return newIterator(c)
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
func (c *BigCache) onEvict(oldestEntry []byte, currentTimestamp uint64, evict func(reason RemoveReason) error) bool {
|
2018-12-19 10:02:07 +00:00
|
|
|
oldestTimestamp := readTimestampFromEntry(oldestEntry)
|
|
|
|
if currentTimestamp-oldestTimestamp > c.lifeWindow {
|
2019-11-04 10:08:22 +00:00
|
|
|
evict(Expired)
|
2018-12-19 10:02:07 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BigCache) cleanUp(currentTimestamp uint64) {
|
|
|
|
for _, shard := range c.shards {
|
|
|
|
shard.cleanUp(currentTimestamp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BigCache) getShard(hashedKey uint64) (shard *cacheShard) {
|
|
|
|
return c.shards[hashedKey&c.shardMask]
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
func (c *BigCache) providedOnRemove(wrappedEntry []byte, reason RemoveReason) {
|
2018-12-19 10:02:07 +00:00
|
|
|
c.config.OnRemove(readKeyFromEntry(wrappedEntry), readEntry(wrappedEntry))
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
func (c *BigCache) providedOnRemoveWithReason(wrappedEntry []byte, reason RemoveReason) {
|
|
|
|
if c.config.onRemoveFilter == 0 || (1<<uint(reason))&c.config.onRemoveFilter > 0 {
|
|
|
|
c.config.OnRemoveWithReason(readKeyFromEntry(wrappedEntry), readEntry(wrappedEntry), reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BigCache) notProvidedOnRemove(wrappedEntry []byte, reason RemoveReason) {
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|