otto

package module
v0.0.63 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

README

Otto

An S3-FIFO-based cache for big values in Go.

package main

import (
	"fmt"

	"github.com/notfilippo/otto"
)

func main() {
    chunkSize := 1024
    chunkCount := 256
    cache := otto.New(chunkSize, chunkCount)

    cache.Set("key", []byte("value"))

    value := cache.Get("key", nil)
    fmt.Println(string(value))
}

Documentation

Index

Constants

View Source
const (
	DefaultSmallQueuePercent = 10
)

Variables

View Source
var ErrAlreadyExists = errors.New("otto: key already exists in cache")
View Source
var ErrClosedCache = errors.New("otto: can't perform operation on closed cache")
View Source
var ErrEmptyValue = errors.New("otto: can't add empty value to cache")
View Source
var ErrTooBig = errors.New("otto: value is too big for cache")

Functions

func SaveToFile added in v0.0.11

func SaveToFile(c Cache, path string) error

SaveToFile serializes the cache as a file in the provided path.

Types

type Cache

type Cache interface {
	// Set inserts an item in the cache. If the cache is full an element
	// will be evicted.
	//
	// NOTE: updates are not supported.
	Set(key string, val []byte) error

	// Get retrieves - if available - an item from the cache. If the item
	// does not exist it will return nil.
	Get(key string, buf []byte) []byte

	// Has checks if an item - identified by its key - is present in the
	// cache.
	Has(key string) bool

	// Clear clears the cache, removing every entry without freeing the memory.
	Clear()

	// Close clears the cache, removing every entry and freeing the memory.
	Close() error

	// Entries returns the number of entries currently stored in the cache.
	Entries() uint64

	// Size returns the sum of the sizes of all the entries currently
	// stored in the cache.
	Size() uint64

	// Capacity returns the theoretical capacity of the cache.
	Capacity() uint64

	// Serialize write the cache to a writer. The cache can then be
	// reinstantiated using the otto.Deserialize function.
	Serialize(w io.Writer) error
}

func Deserialize

func Deserialize(r io.Reader, slotSize, slotCount int32) (Cache, error)

Deserialize deserializes the cache from a byte stream. Refer to the New method for the usage of slotSize & slotCount arguments.

func DeserializeEx added in v0.0.27

func DeserializeEx(r io.Reader, slotSize, mCap, sCap int32) (Cache, error)

DeserializeEx deserializes the cache from a byte stream. Refer to the NewEx method for the usage of slotSize & mCap & sCap arguments.

func DeserializeExV100 added in v0.0.63

func DeserializeExV100(d *gob.Decoder, slotSize, mCap, sCap int32) (Cache, error)

Deserializer for version otto-cache-1.0.0

func DeserializeExV101 added in v0.0.63

func DeserializeExV101(d *gob.Decoder, slotSize, mCap, sCap int32) (Cache, error)

Deserializer for version otto-cache-1.0.1

func LoadFromFile

func LoadFromFile(path string, slotSize, slotCount int32) (Cache, error)

LoadFromFile deserializes the cache from the file at the provided path. Refer to the New method for the usage of slotSize & slotCount arguments.

func LoadFromFileEx added in v0.0.27

func LoadFromFileEx(path string, slotSize, mCap, sCap int32) (Cache, error)

LoadFromFileEx deserializes the cache from the file at the provided path. Refer to the NewEx method for the usage of slotSize & mCap & sCap arguments.

func New

func New(slotSize, slotCap int32) Cache

New creates a new otto.Cache with a fixed size of slotSize * slotCount. Calling the Set method on a full cache will cause elements to be evicted according to the S3-FIFO algorithm.

func NewEx added in v0.0.16

func NewEx(slotSize, mCap, sCap int32) Cache

NewEx creates a new otto.Cache with a fixed size of slotSize * (mCap + sCap). Calling the Set method on a full cache will cause elements to be evicted according to the S3-FIFO algorithm.

mCapacity and sCapacity configure the size of the m and the s queue respectively. To learn more about S3-FIFO visit https://s3fifo.com/

type InternalStatsCache added in v0.0.36

type InternalStatsCache interface {
	Cache

	// MQueueEntries returns the number of entries in the M-queue.
	MQueueEntries() uint64

	// MQueueCapacity returns the capacity of the M-queue.
	MQueueCapacity() uint64

	// SQueueEntries returns the number of entries in the S-queue.
	SQueueEntries() uint64

	// SQueueCapacity returns the capacity of the S-queue.
	SQueueCapacity() uint64
}

type TrackerCache added in v0.0.11

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

func NewTracker added in v0.0.11

func NewTracker(c Cache, windows map[string]TrackerWindow) *TrackerCache

func (*TrackerCache) Capacity added in v0.0.32

func (s *TrackerCache) Capacity() uint64

func (*TrackerCache) Clear added in v0.0.11

func (s *TrackerCache) Clear()

func (*TrackerCache) Close added in v0.0.11

func (s *TrackerCache) Close() error

func (*TrackerCache) Entries added in v0.0.11

func (s *TrackerCache) Entries() uint64

func (*TrackerCache) Get added in v0.0.11

func (s *TrackerCache) Get(key string, buf []byte) []byte

func (*TrackerCache) Has added in v0.0.56

func (s *TrackerCache) Has(key string) bool

func (*TrackerCache) Hits added in v0.0.11

func (s *TrackerCache) Hits() uint64

func (*TrackerCache) Misses added in v0.0.11

func (s *TrackerCache) Misses() uint64

func (*TrackerCache) Serialize added in v0.0.11

func (s *TrackerCache) Serialize(w io.Writer) error

func (*TrackerCache) Set added in v0.0.11

func (s *TrackerCache) Set(key string, val []byte) error

func (*TrackerCache) Sets added in v0.0.11

func (s *TrackerCache) Sets() uint64

func (*TrackerCache) Size added in v0.0.32

func (s *TrackerCache) Size() uint64

func (*TrackerCache) Window added in v0.0.11

func (s *TrackerCache) Window(name string) (uint64, bool)

type TrackerWindow added in v0.0.11

type TrackerWindow struct {
	BucketDuration time.Duration
	BucketCount    int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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