cache

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 11 Imported by: 0 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.

Documentation

Overview

Package cache provides the storage layer behind mirava-core probes: a small interface implemented by a bounded in-memory LRU and an atomic-write disk layer, plus a singleflight guard so concurrent lookups share one upstream fetch.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDir

func DefaultDir() string

DefaultDir returns the OS cache location for mirava-core, e.g. ~/Library/Caches/mirava-core on macOS or ~/.cache/mirava-core on Linux.

Types

type Disk

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

Disk is a persistent store under a root directory. Files are written atomically (temp file + rename) so a crash — or a second process sharing the cache — never observes a torn value.

func OpenDisk

func OpenDisk(dir string) (*Disk, error)

OpenDisk opens (creating if needed) a disk store rooted at dir.

func (*Disk) Delete

func (d *Disk) Delete(key string)

func (*Disk) Get

func (d *Disk) Get(key string) (Entry, bool)

func (*Disk) Prune

func (d *Disk) Prune(now time.Time) (removed int)

Prune walks the store and removes expired records. It is best-effort and intended to run occasionally (e.g. on process start).

func (*Disk) Put

func (d *Disk) Put(key string, e Entry)

type Entry

type Entry struct {
	Value     []byte
	ExpiresAt time.Time
}

Entry is a stored value with its expiry. Zero ExpiresAt means no expiry.

func (Entry) Fresh

func (e Entry) Fresh(now time.Time) bool

Fresh reports whether the entry is usable at time now.

type LRU

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

LRU is a bounded least-recently-used in-memory store. Entries are evicted when either the entry count or total byte size exceeds the limits.

func NewLRU

func NewLRU(maxEntries int, maxBytes int64) *LRU

NewLRU builds an in-memory store. Non-positive maxEntries defaults to 4096; non-positive maxBytes defaults to 256 MiB.

func (*LRU) Delete

func (c *LRU) Delete(key string)

func (*LRU) Get

func (c *LRU) Get(key string) (Entry, bool)

func (*LRU) Put

func (c *LRU) Put(key string, e Entry)

type Layered

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

Layered composes a fast store (memory) over a slower one (disk). Gets promote disk hits into the fast layer WITHOUT extending their original expiry — the disk entry's ExpiresAt is preserved exactly.

func NewLayered

func NewLayered(fast, slow Store) *Layered

NewLayered builds a two-tier store.

func (*Layered) Delete

func (l *Layered) Delete(key string)

func (*Layered) Get

func (l *Layered) Get(key string) (Entry, bool)

func (*Layered) Put

func (l *Layered) Put(key string, e Entry)

type Store

type Store interface {
	// Get returns the entry and whether it was present at all. Expiry is
	// checked by callers (so promotion never re-extends TTL).
	Get(key string) (Entry, bool)
	// Put stores the entry, replacing any previous value for key.
	Put(key string, e Entry)
	// Delete removes key if present.
	Delete(key string)
}

Store is the cache contract. Implementations must be safe for concurrent use. mirava-core provides LRU (memory) and Disk; callers may supply their own (e.g. Redis-backed) via mirava.WithCache.

Jump to

Keyboard shortcuts

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