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.
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) Get ¶
Get implements the Interface interface for Empty. It always returns nil and false.
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.