cache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cache provides a cache of an event stream's most recent events.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache holds the most recent events from one event stream that match a known set of message types.

The cache holds a contiguous segment of the stream, [begin, end), within which it holds every event of those types and no others, so the events it holds may have gaps between their offsets. The cache's segment ends where the last events appended to the cache ended, and begins after the last event that the cache evicted.

A cache has one writer, which resets it with Cache.Reset and appends to it with Cache.Append, and any number of readers, which observe its segment through Cache.Segment.

func New

func New(capacity int, begin uint64) *Cache

New returns an empty cache. It holds at most the given number of events, and its segment begins and ends at the given offset.

func (*Cache) Append

func (c *Cache) Append(begin, end uint64, envelopes []*envelopepb.Envelope)

Append adds the events in the range [begin, end) of the stream to the cache.

The range must be contiguous with the cache's segment, so the given begin offset must be the offset at which that segment currently ends. The segment then extends to the given end offset.

If the cache is then over capacity, it evicts its oldest events, and its segment begins after the last of them.

Each envelope must have the envelopepb.EventStreamPosition extension set, and the envelopes must be in offset order.

func (*Cache) Reset

func (c *Cache) Reset(begin uint64)

Reset empties the cache, so that its segment begins and ends at the given offset.

func (*Cache) Segment

func (c *Cache) Segment() cosyne.ObservableR[Segment]

Segment returns an observable of the segment that the cache holds.

type Relation

type Relation int

Relation describes where a checkpoint lies in relation to a Segment.

Its values follow the convention of cmp.Compare: negative if the checkpoint is before the segment, zero if it is within it, and positive if it is after it.

const (
	// Before means that the checkpoint is before the segment, so the events
	// from the checkpoint must be read from history.
	Before Relation = -1

	// Within means that the checkpoint is inside the segment, so the segment
	// holds the events from the checkpoint.
	Within Relation = 0

	// After means that the checkpoint is at or beyond the end of the segment,
	// so there is nothing to deliver until the cache is appended to past it.
	After Relation = +1
)

type Segment

type Segment struct {
	// Begin and End bound the segment, [Begin, End).
	Begin, End uint64
	// contains filtered or unexported fields
}

Segment is the segment of the stream that a Cache holds, as an immutable snapshot.

func (Segment) Read

func (s Segment) Read(
	cp uint64,
	types *uuidcollection.Set,
) ([]*envelopepb.Envelope, Relation)

Read returns the events in the segment from the given checkpoint.

Only events of the given types are returned. It also returns where the checkpoint lies in relation to the segment, and the events are meaningful only if the checkpoint is Within it.

Jump to

Keyboard shortcuts

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