rotcache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2022 License: MIT Imports: 5 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.

README

Rotating cache

Byte cache implementation with lock-free access. Designed to work with small data under high pressure. Lock-free access (both read and write) provides by using two shards of data: one for write, another for read. After finishing writes cache rotates (swaps) shards.

Usage

c := rotcache.RotCache{Hasher: fnv.Hasher{}}

go func() {
    for {
        c.Prepare()
        _ = c.SetString("foo", "bar")
        _ = c.ISetString(111, "asd")
        _ = c.SetString("qwe", "rty")
        c.Rotate()
        time.Sleep(time.Second)
        // cancellation logic ...
    }
}()

go func() {
	for {
        v, _ := c.GetString("qwe")
        log.Println(v) // "rty"
        // cancellation logic ...
    }()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoHasher    = errors.New("no hasher provided")
	ErrNoKey       = errors.New("no keys provided")
	ErrNoValue     = errors.New("no value provided")
	ErrKeyNotFound = errors.New("key not found")
)

Functions

This section is empty.

Types

type RotCache

type RotCache struct {
	// Keys hasher. Uses to convert string keys to hashes.
	Hasher hash.Hasher
	// contains filtered or unexported fields
}

RotCache is a rotating cache implementation.

func (*RotCache) Get

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

Get gets stored value by key from the actual cache.

func (*RotCache) GetString

func (c *RotCache) GetString(key string) (string, error)

GetString gets stored value as string.

func (*RotCache) IGet

func (c *RotCache) IGet(key int64) ([]byte, error)

IGet get stored value using int64 key.

func (*RotCache) IGetString

func (c *RotCache) IGetString(key int64) (s string, err error)

IGetString get stored value as string using int64 key.

func (*RotCache) ISet

func (c *RotCache) ISet(key int64, val []byte) error

ISet sets new value with int64 key.

func (*RotCache) ISetString

func (c *RotCache) ISetString(key int64, val string) error

ISetString sets string value with int64 key.

func (*RotCache) Prepare

func (c *RotCache) Prepare()

Prepare resets the opposite cache.

func (*RotCache) Rotate

func (c *RotCache) Rotate()

Rotate swaps actual and opposite caches.

func (*RotCache) Set

func (c *RotCache) Set(key string, val []byte) error

Set sets new key-value pair to the opposite cache.

func (*RotCache) SetString

func (c *RotCache) SetString(key, val string) error

SetString sets string value with string key.

func (*RotCache) UGet

func (c *RotCache) UGet(key uint64) ([]byte, error)

UGet get stored value using uint64 key.

func (*RotCache) UGetString

func (c *RotCache) UGetString(key uint64) (s string, err error)

UGetString get stored value as string using uint64 key.

func (*RotCache) USet

func (c *RotCache) USet(key uint64, val []byte) error

USet sets new value with uint64 key.

func (*RotCache) USetString

func (c *RotCache) USetString(key uint64, val string) error

USetString sets string value with uint64 key.

Jump to

Keyboard shortcuts

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