bytecache

package
v0.35.21 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Unlicense Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package bytecache provides a simple LRU cache implementation that takes size into account.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Default

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

Default is the default byte cache implementation. It is safe for concurrent use.

func NewDefault

func NewDefault(conf *DefaultConfig) (c *Default)

NewDefault creates a new properly initialized default byte cache. If conf is nil, defaults are used. If conf is not nil, it must be valid.

func (*Default) Clear

func (c *Default) Clear()

Clear implements the Interface interface for *Default.

func (*Default) Del

func (c *Default) Del(key []byte)

Del implements the Interface interface for *Default.

func (*Default) Get

func (c *Default) Get(key []byte) (val []byte, ok bool)

Get implements the Interface interface for *Default.

func (*Default) Set

func (c *Default) Set(key, val []byte)

Set implements the Interface interface for *Default.

func (*Default) Stats

func (c *Default) Stats() (s Stats)

Stats implements the Interface interface for *Default.

type DefaultConfig

type DefaultConfig struct {
	// OnDelete, if non-nil, is called after an element has been evicted during
	// [Default.Set].  OnDelete must not call any methods on cache.
	OnDelete OnDeleteFunc

	// MaxSize is the maximum size of the cache for both keys and values.  Zero
	// means no limit.
	MaxSize datasize.ByteSize

	// MaxElementSize is the maximum size of an element, including the key and
	// the value.  Elements larger than this are dropped.  Zero means equal to
	// [DefaultConfig.MaxSize].  It must be no greater than
	// [DefaultConfig.MaxSize] unless the latter is zero.
	MaxElementSize datasize.ByteSize

	// MaxCount is the maximum number of elements.  Zero means no limit.
	MaxCount uint64
}

DefaultConfig defines the configuration for the default byte cache.

type Empty

type Empty struct{}

Empty is an Interface implementation that does nothing.

func (Empty) Clear

func (Empty) Clear()

Clear implements the Interface interface for Empty.

func (Empty) Del

func (Empty) Del(key []byte)

Del implements the Interface interface for Empty.

func (Empty) Get

func (Empty) Get(key []byte) (val []byte, ok bool)

Get implements the Interface interface for Empty. It always returns nil and false.

func (Empty) Set

func (Empty) Set(key, val []byte)

Set implements the Interface interface for Empty.

func (Empty) Stats

func (Empty) Stats() (s Stats)

Stats implements the Interface interface for Empty. s is always empty.

type Interface

type Interface interface {
	// Clear all data and statistics.
	Clear()

	// Del deletes the cached data.
	Del(key []byte)

	// Get returns the cached data.  val must not be modified.
	Get(key []byte) (val []byte, ok bool)

	// Set sets the data.  key and val must not be modified after calling Set.
	Set(key, val []byte)

	// Stats returns the current cache statistics.  The statistics are not
	// guaranteed to be consistent.
	Stats() (s Stats)
}

Interface is the byte cache interface. All methods must be safe for concurrent use.

type OnDeleteFunc

type OnDeleteFunc func(key, val []byte)

OnDeleteFunc is a type for callback functions called after an element has been deleted automatically. The function should not be long-running. The function must not retain key and val. The function must not call any methods on the cache.

TODO(a.garipov): Consider making a larger interface to make the cache more observable.

type Stats

type Stats struct {
	// Size is the current size of the byte cache.
	Size datasize.ByteSize

	// Count is the current number of items in the cache.
	Count uint64

	// Hit is the number of cache hits.
	Hit uint64

	// Miss is the number of cache misses.
	Miss uint64
}

Stats represents statistics for the cache.

Jump to

Keyboard shortcuts

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