Skip to content

logbn/mvfifo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A multi value FIFO cache

Resources

License

Stars

Watchers

Forks

Packages

No packages published