mvfifo

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

Multi Value FIFO Cache

A multi value FIFO cache for byte slices.

Go Reference License Go Report Card Go Coverage

This multi value FIFO cache was created to maintain an in memory segment of an event stream distributed across an array of topics. Items are evicted in the order in which they are inserted. The length of the cache is bound by the total size of all items in the cache rather than the total number of items in the cache to more easily manage memory usage.

Usage

The maximum size of the cache can be specified in bytes (default 256 MiB).

import "github.com/logbn/mvfifo"

c := mvfifo.NewCache(
    mvfifo.WithMaxSizeBytes(1 << 30), // 1 GiB
)

Values can be inserted with cursor data

c.Add("test-1", 1, []byte("test-value-a"))
c.Add("test-1", 2, []byte("test-value-b"))
c.Add("test-2", 3, []byte("test-value-c"))

Values can be iterated for any key

for cursor, value := range c.Iter("test-1") {
    println(cursor, string(value))
}
// output:
//   1 test-a
//   2 test-b

for cursor, value := range c.Iter("test-2") {
    println(cursor, string(value))
}
// output:
//   3 test-c

Values can be iterated for any key after a specified cursor value (non-inclusive)

for cursor, value := range c.IterAfter("test-1", 1) {
    println(cursor, string(value))
}
// output:
//   2 test-b

The cursor is used only for range iteration, not for sorting.
The cache makes no attempt to reorder values based on the value of inserted cursors.
The calling code is responsible for ensuring that cursors increase over time and never decrease.

Concurrency

This package is thread safe.

License

Multi Value FIFO is licensed under Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultMaxSizeBytes sets the default max size at 256 MiB
	DefaultMaxSizeBytes = 256 << 20
)

Functions

This section is empty.

Types

type Cache

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

Cache implements a multi value FIFO cache.

func NewCache

func NewCache(opts ...Option) *Cache

NewCache returns a new Cache.

func (*Cache) Add

func (c *Cache) Add(key string, cur uint64, val []byte)

Add adds an item to the cache by key, cursor and value.

func (*Cache) First

func (c *Cache) First() (cur uint64, val []byte)

First returns the oldest cursor and value in the cache.

func (*Cache) Iter

func (c *Cache) Iter(key string) iter.Seq2[uint64, []byte]

Iter returns an iterator over the cache at a certain key.

func (*Cache) IterAfter

func (c *Cache) IterAfter(key string, cur uint64) iter.Seq2[uint64, []byte]

IterAfter returns an iterator over the cache at a certain key after a specific cursor. Complexity is O(2n). We iterate backward over the list then forward to optimize for access to later values.

func (*Cache) Last

func (c *Cache) Last() (cur uint64, val []byte)

Last returns the newest cursor and value in the cache.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Resize

func (c *Cache) Resize(maxBytes int)

Resize changes the maximum size of the cache.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the approximate size of the cache in bytes.

type Option

type Option func(*Cache)

func WithMaxSizeBytes

func WithMaxSizeBytes(s int) Option

Jump to

Keyboard shortcuts

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