Documentation
¶
Index ¶
- Constants
- Variables
- type Cache
- func (c *Cache) Close() error
- func (c *Cache) Delete(key string) error
- func (c *Cache) Extract(key string) ([]byte, error)
- func (c *Cache) ExtractTo(dst []byte, key string) ([]byte, error)
- func (c *Cache) ExtractToWithUnmarshaller(dst []byte, key string, u Unmarshaller) (err error)
- func (c *Cache) ExtractWithUnmarshaller(key string, u Unmarshaller) error
- func (c *Cache) Get(key string) ([]byte, error)
- func (c *Cache) GetTo(dst []byte, key string) ([]byte, error)
- func (c *Cache) GetToWithUnmarshaller(dst []byte, key string, u Unmarshaller) (err error)
- func (c *Cache) GetWithUnmarshaller(key string, u Unmarshaller) error
- func (c *Cache) Release() error
- func (c *Cache) Reset() error
- func (c *Cache) Set(key string, data []byte) error
- func (c *Cache) SetMarshallerTo(key string, m MarshallerTo) error
- func (c *Cache) Size() (r CacheSize)
- type CacheSize
- type Clock
- type Config
- type DummyMetrics
- func (DummyMetrics) Alloc(_ string, _ uint32)
- func (DummyMetrics) Collision(_ string)
- func (DummyMetrics) Corrupt(_ string)
- func (DummyMetrics) Del(_ string)
- func (DummyMetrics) Dump(_ string)
- func (DummyMetrics) Evict(_ string, _ bool)
- func (DummyMetrics) Expire(_ string)
- func (DummyMetrics) Fill(_ string, _ uint32)
- func (DummyMetrics) Hit(_ string, _ time.Duration)
- func (DummyMetrics) Load(_ string)
- func (DummyMetrics) Miss(_ string)
- func (DummyMetrics) NoSpace(_ string)
- func (DummyMetrics) Release(_ string, _ uint32)
- func (DummyMetrics) Reset(_ string, _ uint32)
- func (DummyMetrics) Set(_ string, _ time.Duration)
- type DumpReader
- type DumpWriter
- type Enqueuer
- type Entry
- type Listener
- type Logger
- type MarshallerTo
- type MemorySize
- type MetricsWriter
- type NativeClock
- type Unmarshaller
Constants ¶
const ( VacuumRatioWeak = .25 VacuumRatioModerate = .5 VacuumRatioAggressive = .75 )
const ( MaxBucketSize = math.MaxUint32 MaxKeySize = math.MaxUint16 MinExpireInterval = time.Second )
const ( Byte MemorySize = 1 Kilobyte = Byte * 1024 Megabyte = Kilobyte * 1024 Gigabyte = Megabyte * 1024 Terabyte = Gigabyte * 1024 )
Common sizes.
Variables ¶
var ( ErrOK error = nil ErrBadConfig = errors.New("config is empty") ErrBadCache = errors.New("cache uninitialized, use New()") ErrCacheClosed = errors.New("cache closed") ErrBadHasher = errors.New("you must provide hasher helper") ErrBadBuckets = errors.New("buckets count must be greater than zero") ErrKeyTooBig = fmt.Errorf("key overflows maximum %d", MaxKeySize) ErrNotFound = errors.New("entry not found") ErrEntryExists = errors.New("entry already exists") ErrEntryTooBig = errors.New("entry too big") ErrEntryEmpty = errors.New("entry is empty") ErrEntryCorrupt = errors.New("entry corrupted") ErrEntryCollision = errors.New("entry keys collision") ErrExpireDur = errors.New("expire interval is too short") ErrVacuumDur = errors.New("vacuum interval must be greater than expire interval") ErrBucketService = errors.New("cache bucket is under maintenance") ErrBucketCorrupt = errors.New("cache bucket is corrupted") ErrNoSpace = errors.New("no space available") ErrNoEnqueuer = errors.New("no enqueuer provided") ErrNoUnmarshaller = errors.New("no unmarshaller provided") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a byte cache implementation based on cbyte package.
func (*Cache) ExtractToWithUnmarshaller ¶
func (c *Cache) ExtractToWithUnmarshaller(dst []byte, key string, u Unmarshaller) (err error)
ExtractToWithUnmarshaller extracts entry bytes and apply unmarshal logic.
func (*Cache) ExtractWithUnmarshaller ¶
func (c *Cache) ExtractWithUnmarshaller(key string, u Unmarshaller) error
ExtractWithUnmarshaller extracts entry bytes and apply unmarshal logic.
func (*Cache) GetToWithUnmarshaller ¶
func (c *Cache) GetToWithUnmarshaller(dst []byte, key string, u Unmarshaller) (err error)
GetToWithUnmarshaller gets entry bytes to dst and apply unmarshal logic.
func (*Cache) GetWithUnmarshaller ¶
func (c *Cache) GetWithUnmarshaller(key string, u Unmarshaller) error
GetWithUnmarshaller gets entry bytes and apply unmarshal logic.
func (*Cache) SetMarshallerTo ¶
func (c *Cache) SetMarshallerTo(key string, m MarshallerTo) error
SetMarshallerTo sets entry like protobuf object to the cache.
type CacheSize ¶
type CacheSize struct {
// contains filtered or unexported fields
}
CacheSize represents memory size types of cache: total, used and free.
type Clock ¶
type Clock interface {
// Start the clock.
Start()
// Stop the clock.
Stop()
// Active checks if close is started.
Active() bool
// Now returns current time considering jumps.
Now() time.Time
// Jump performs time travel to delta (maybe negative). Now and scheduled jobs considers jumps.
Jump(delta time.Duration)
// Schedule registers fn to call at every d.
Schedule(d time.Duration, fn func())
}
Clock describes timer helper with testing features (like Jump).
type Config ¶
type Config struct {
// Capacity represents maximum cache payload size (it doesn't consider index size).
Capacity MemorySize
// ArenaCapacity determines fixed memory arena size.
// If this param omit defaultArenaCapacity (16KB) will use instead.
ArenaCapacity MemorySize
// Hasher calculates uint64 hash of entries keys.
// Mandatory param.
Hasher hash.Hasher
// Buckets represents buckets (data shards) count. Must be power of two.
// Mandatory param.
Buckets uint
// ExpireInterval represents entry lifetime period.
// After this period entry may be evicted in any time. Reading of entry after that period will fail as expired.
// Mandatory param.
ExpireInterval time.Duration
// EvictInterval represents period between eviction operations.
// If this param omit ExpireInterval will use instead.
EvictInterval time.Duration
// EvictWorkers limits workers count for evict operation.
// If this param omit defaultEvictWorkers (16) will use instead.
EvictWorkers uint
// VacuumInterval represents period between vacuum operations.
VacuumInterval time.Duration
// VacuumWorkers limits workers count for vacuum operation.
// If this param omit defaultVacuumWorkers (16) will use instead.
VacuumWorkers uint
// VacuumRatio represents how many of memory available to release must be released. Available range is [0:1.0].
// There are three predefined ratios:
// * VacuumRatioWeak - release 25% of memory available to release
// * VacuumRatioModerate - release 50%
// * VacuumRatioAggressive - release 75%
// Lower value will trigger reallocation rarely. Higher value will keep the lowest cache size, but causes often
// reallocations.
// If this param omit VacuumRatioModerate (50%) will use instead.
VacuumRatio float64
// ResetWorkers limits workers count for reset operation.
// If this param omit defaultResetWorkers (16) will use instead.
ResetWorkers uint
// ReleaseWorkers limits workers count for release operation.
// If this param omit defaultReleaseWorkers (16) will use instead.
ReleaseWorkers uint
// CollisionCheck enables collision checks.
CollisionCheck bool
// Clock implementation.
// If this param omit nativeClock{} will use instead.
Clock Clock
// ExpireListener triggers on every expired item.
ExpireListener Listener
// DumpWriter represents writer for dumps.
DumpWriter DumpWriter
// DumpInterval indicates how often need dump cache data.
DumpInterval time.Duration
// DumpWriteWorkers limits workers count that sends entries to DumpWriter.
// If this param omit defaultDumpWriteWorkers (16) will use instead.
DumpWriteWorkers uint
// DumpReader represents dump loader that fills cache with dumped data.
DumpReader DumpReader
// DumpReadBuffer represents how many items from dump may be processed at once.
DumpReadBuffer uint
// DumpReadWorkers limits workers count that processes entries comes from DumpReader.
// If this param omit defaultDumpReadWorkers (16) will use instead.
DumpReadWorkers uint
// Load dump data asynchronously.
DumpReadAsync bool
// Metrics writer handler.
MetricsWriter MetricsWriter
// Logger is a logging interface to display verbose messages.
Logger Logger
}
Config describes cache properties and behavior.
func DefaultConfig ¶
DefaultConfig makes config with default params.
type DummyMetrics ¶
type DummyMetrics struct{}
func (DummyMetrics) Alloc ¶
func (DummyMetrics) Alloc(_ string, _ uint32)
func (DummyMetrics) Collision ¶
func (DummyMetrics) Collision(_ string)
func (DummyMetrics) Corrupt ¶
func (DummyMetrics) Corrupt(_ string)
func (DummyMetrics) Del ¶
func (DummyMetrics) Del(_ string)
func (DummyMetrics) Dump ¶
func (DummyMetrics) Dump(_ string)
func (DummyMetrics) Evict ¶
func (DummyMetrics) Evict(_ string, _ bool)
func (DummyMetrics) Expire ¶
func (DummyMetrics) Expire(_ string)
func (DummyMetrics) Fill ¶
func (DummyMetrics) Fill(_ string, _ uint32)
func (DummyMetrics) Load ¶
func (DummyMetrics) Load(_ string)
func (DummyMetrics) Miss ¶
func (DummyMetrics) Miss(_ string)
func (DummyMetrics) NoSpace ¶
func (DummyMetrics) NoSpace(_ string)
func (DummyMetrics) Release ¶
func (DummyMetrics) Release(_ string, _ uint32)
func (DummyMetrics) Reset ¶
func (DummyMetrics) Reset(_ string, _ uint32)
type DumpReader ¶
type DumpReader interface {
// Read reads entry from underlying data stream.
// It returns entry and any error encountered.
Read() (Entry, error)
}
DumpReader is the interface that wraps the basic Read method.
type DumpWriter ¶
type DumpWriter interface {
// Write writes entry data to the underlying data stream.
// It returns the number of bytes written from entry and any error encountered.
Write(entry Entry) (int, error)
// Flush indicates to writer that all cache data was dumped.
Flush() error
}
DumpWriter is the interface that wraps the basic Write method.
type Enqueuer ¶
Enqueuer is the interface that wraps the basic Enqueue method.
Uses for queue.DumpWriter and queue.Listener that contains underlying queue.
type MarshallerTo ¶
MarshallerTo interface to write struct like Protobuf.
type MetricsWriter ¶
type MetricsWriter interface {
// Alloc registers how many arenas allocates and increase cache size (total and free).
Alloc(bucket string, size uint32)
// Fill registers how many arenas fills and calculate cache size (inc fill and dec free).
Fill(bucket string, size uint32)
// Reset registers how many arenas resets and calculate cache size (inc free and dec used).
Reset(bucket string, size uint32)
// Release registers how many arenas releases and calculate cache size (dec total and free).
Release(bucket string, size uint32)
// Set registers how many entries writes to cache.
Set(bucket string, dur time.Duration)
// Hit registers how many entries reads from cache.
Hit(bucket string, dur time.Duration)
// Del registers how many entries deletes from cache.
Del(bucket string)
// Evict registers how many entries evicts from cache.
// Param alive indicates is entry deleted or not.
Evict(bucket string, alive bool)
// Miss registers how many reads failed due to not found error.
Miss(bucket string)
// Expire registers how many entries in cache marks as expired.
Expire(bucket string)
// Corrupt registers how many entries reads failed due to corruption error.
Corrupt(bucket string)
// Collision registers how many keys collisions triggers due to hasher.
Collision(bucket string)
// NoSpace registers how many writes failed due to no space error.
NoSpace(bucket string)
// Dump registers how many entries dumped.
Dump(bucket string)
// Load registers how many entries loaded from dump.
Load(bucket string)
}
MetricsWriter is the interface that wraps the basic metrics methods.
type NativeClock ¶
type NativeClock struct {
// contains filtered or unexported fields
}
NativeClock is a primitive clock based on time package.
Jump doesn't work in that implementation.
func (*NativeClock) Active ¶
func (n *NativeClock) Active() bool
func (*NativeClock) Jump ¶
func (n *NativeClock) Jump(_ time.Duration)
func (*NativeClock) Now ¶
func (n *NativeClock) Now() time.Time
func (*NativeClock) Schedule ¶
func (n *NativeClock) Schedule(d time.Duration, fn func())
func (*NativeClock) Start ¶
func (n *NativeClock) Start()
func (*NativeClock) Stop ¶
func (n *NativeClock) Stop()
type Unmarshaller ¶
Unmarshaller is the interface that wraps the basic Unmarshal method.