cache

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 3 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 defines a generic key-value cache interface used across the TNET project — HTTP response caching, and (as consumers migrate) DNS responses, routes, and other []byte-serializable data.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("cache: key not found")

ErrNotFound is returned by Cache.Get when the key does not exist. An expired-but-present entry is NOT a miss: it is returned with a non-nil Entry (Expired() == true) so callers can implement serve-stale.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get returns the entry for key, or ErrNotFound if the key is absent.
	Get(ctx context.Context, key string) (*Entry, error)
	// Set stores data under key with the given options.
	Set(ctx context.Context, key string, data []byte, opts ...SetOption) error
	// Delete removes key from the cache. Deleting an absent key is a no-op.
	Delete(ctx context.Context, key string) error
}

Cache is a concurrency-safe []byte key-value store. Implementations must be safe for concurrent use.

Get returns ErrNotFound only when the key is absent; an expired entry is returned (with Expired() == true) to support serve-stale.

type Entry

type Entry struct {
	// Data is the cached payload. Callers serialize/deserialize their own types.
	Data []byte
	// Expiration is the absolute expiry time. A zero value means the entry
	// never expires.
	Expiration time.Time
}

Entry is a single cached value with an expiration time.

func (*Entry) Expired

func (e *Entry) Expired() bool

Expired reports whether the entry has passed its expiration time. A zero Expiration (never expires) is never expired.

func (*Entry) TTL

func (e *Entry) TTL() time.Duration

TTL returns the entry's remaining time to live. It returns -1 for an entry that never expires, and a non-positive duration for an expired entry.

type SetOption

type SetOption func(opts *SetOptions)

SetOption is a functional option for configuring SetOptions.

func WithTTL

func WithTTL(d time.Duration) SetOption

WithTTL sets the time to live for a cache entry, overriding the cache default.

type SetOptions

type SetOptions struct {
	// TTL is the time to live for the entry. A non-positive TTL uses the
	// cache's default TTL.
	TTL time.Duration
}

SetOptions holds parameters for a Set call.

Jump to

Keyboard shortcuts

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