cache

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0, MIT Imports: 10 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.

Documentation

Overview

Package cache is Fletch's byte cache contract with a bounded in-process LRU and a table-backed store over an app-supplied *sql.DB. It depends on nothing outside the standard library; Redis lives in cache/redis.

Index

Constants

View Source
const DefaultMaxBytes = 64 << 20

DefaultMaxBytes is the default bound of the in-memory cache.

View Source
const Redacted = "[redacted]"

Redacted replaces secret setting values in a Description.

Variables

This section is empty.

Functions

func AppendGet

func AppendGet(ctx context.Context, c Cache, dst []byte, key string) ([]byte, bool, error)

AppendGet appends the value of key in c to dst, through Appender when c implements it and through Get otherwise.

Types

type Appender

type Appender interface {
	// AppendGet appends the value of key to dst and returns the extended
	// slice and true, or dst unchanged and false for a missing or expired
	// key.
	AppendGet(ctx context.Context, dst []byte, key string) ([]byte, bool, error)
}

Appender is implemented by caches that can copy a value into a buffer the caller owns, such as Memory, so reading many values costs no allocation per value.

type Cache

type Cache interface {
	// Get returns the value and true, or false for a missing or expired key.
	Get(ctx context.Context, key string) ([]byte, bool, error)
	// Set stores val. A ttl of zero or less means the entry never expires.
	Set(ctx context.Context, key string, val []byte, ttl time.Duration) error
	// Delete removes the key. Deleting a missing key is not an error.
	Delete(ctx context.Context, key string) error
	// Describe reports the provider for AX inspection.
	Describe() Description
}

Cache stores byte values by key with an optional time to live. Implementations must not do I/O in their constructors.

func Memory

func Memory(opts ...MemoryOption) Cache

Memory returns a bounded in-process cache with least-recently-used eviction, approximated per shard by the second-chance (CLOCK) algorithm. Keys are spread over shards whose reads share a read lock, so concurrent hits never wait on each other.

func SQLite

func SQLite(db *sql.DB, opts ...SQLiteOption) Cache

SQLite returns a cache stored in a table of db, which the app owns and opens with a SQLite driver. The table is created on first use.

type Description

type Description struct {
	Provider string            `json:"provider"`
	Settings map[string]string `json:"settings,omitempty"`
}

Description is what a Cache reports about itself for `fletch ax inspect`. Settings are already redacted.

type MemoryOption

type MemoryOption func(*memoryConfig)

MemoryOption configures Memory.

func WithClock

func WithClock(now func() time.Time) MemoryOption

WithClock sets the clock used for expiry, for deterministic tests.

func WithMaxBytes

func WithMaxBytes(n int64) MemoryOption

WithMaxBytes bounds the total size of keys and values. Values of zero or less keep the default.

type SQLiteOption

type SQLiteOption func(*sqliteConfig)

SQLiteOption configures SQLite.

func WithSQLiteClock

func WithSQLiteClock(now func() time.Time) SQLiteOption

WithSQLiteClock sets the clock used for expiry, for deterministic tests.

func WithTable

func WithTable(name string) SQLiteOption

WithTable sets the table name. The default is fletch_cache.

Directories

Path Synopsis
Package cachetest is the conformance suite every cache.Cache must pass.
Package cachetest is the conformance suite every cache.Cache must pass.
postgres module
redis module
sqlitecheck module

Jump to

Keyboard shortcuts

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