cbytecache

package module
v0.0.0-...-b018407 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2024 License: MIT Imports: 18 Imported by: 3

README

CByteCache

Byte cache based on cbyte package.

todo translate readme

Documentation

Index

Constants

View Source
const (
	VacuumRatioWeak       = .25
	VacuumRatioModerate   = .5
	VacuumRatioAggressive = .75
)
View Source
const (
	MaxBucketSize = math.MaxUint32
	MaxKeySize    = math.MaxUint16

	MinExpireInterval = time.Second
)
View Source
const (
	Byte     MemorySize = 1
	Kilobyte            = Byte * 1024
	Megabyte            = Kilobyte * 1024
	Gigabyte            = Megabyte * 1024
	Terabyte            = Gigabyte * 1024
)

Common sizes.

Variables

View Source
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 New

func New(conf *Config) (*Cache, error)

New makes cache instance according config.

func (*Cache) Close

func (c *Cache) Close() error

Close destroys cache and releases all data.

You cannot use cache after that.

func (*Cache) Delete

func (c *Cache) Delete(key string) error

Delete removes entry from cache.

func (*Cache) Extract

func (c *Cache) Extract(key string) ([]byte, error)

Extract gets entry bytes by key and remove entry afterward.

func (*Cache) ExtractTo

func (c *Cache) ExtractTo(dst []byte, key string) ([]byte, error)

ExtractTo gets entry bytes to dst and remove it afterward.

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) Get

func (c *Cache) Get(key string) ([]byte, error)

Get gets entry bytes by key.

func (*Cache) GetTo

func (c *Cache) GetTo(dst []byte, key string) ([]byte, error)

GetTo gets entry bytes to dst.

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) Release

func (c *Cache) Release() error

Release releases all cache data.

func (*Cache) Reset

func (c *Cache) Reset() error

Reset performs force eviction of all check entries.

func (*Cache) Set

func (c *Cache) Set(key string, data []byte) error

Set sets entry bytes to the cache.

func (*Cache) SetMarshallerTo

func (c *Cache) SetMarshallerTo(key string, m MarshallerTo) error

SetMarshallerTo sets entry like protobuf object to the cache.

func (*Cache) Size

func (c *Cache) Size() (r CacheSize)

Size returns cache size snapshot. Contains total, used and free sizes.

type CacheSize

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

CacheSize represents memory size types of cache: total, used and free.

func (CacheSize) Equal

func (s CacheSize) Equal(x CacheSize) bool

Equal check is x is equal to s.

func (CacheSize) Free

func (s CacheSize) Free() MemorySize

Free returns free size of cache.

func (CacheSize) String

func (s CacheSize) String() string

String returns a string representation of size.

func (CacheSize) Total

func (s CacheSize) Total() MemorySize

Total returns total size of cache.

func (CacheSize) Used

func (s CacheSize) Used() MemorySize

Used returns used size of cache.

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

func DefaultConfig(expire time.Duration, hasher hash.Hasher, capacity MemorySize) *Config

DefaultConfig makes config with default params.

func (*Config) Copy

func (c *Config) Copy() *Config

Copy copies config instance to protect cache from changing params after start. All config modifications will have no effect after copy.

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) Hit

func (DummyMetrics) Hit(_ string, _ time.Duration)

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)

func (DummyMetrics) Set

func (DummyMetrics) Set(_ string, _ time.Duration)

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

type Enqueuer interface {
	Enqueue(any) error
}

Enqueuer is the interface that wraps the basic Enqueue method.

Uses for queue.DumpWriter and queue.Listener that contains underlying queue.

type Entry

type Entry struct {
	Key    string
	Body   []byte
	Expire uint32
}

Entry represent external entry object.

func (Entry) Copy

func (e Entry) Copy() Entry

Copy copies entry to avoid overwrite entry data.

func (Entry) Size

func (e Entry) Size() int

Size returns entry size in bytes.

type Listener

type Listener interface {
	Listen(entry Entry) error
}

Listener is the interface that wraps the basic Listen method.

type Logger

type Logger interface {
	Printf(format string, v ...any)
	Print(v ...any)
	Println(v ...any)
}

Logger is the interface that wraps the basic logging methods.

type MarshallerTo

type MarshallerTo interface {
	Size() int
	MarshalTo([]byte) (int, error)
}

MarshallerTo interface to write struct like Protobuf.

type MemorySize

type MemorySize uint64

MemorySize represents size in bytes.

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

type Unmarshaller interface {
	Unmarshal([]byte) error
}

Unmarshaller is the interface that wraps the basic Unmarshal method.

Directories

Path Synopsis
metrics
log module
prometheus module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL