Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package cache provides shared distributed caching infrastructure.

It centralizes the TTL-bucketed key lifecycle used by groupcache-based distributed caches throughout parsec, ensuring consistent clock injection, key formatting, and time-bucket rounding.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendTTLSuffix

func AppendTTLSuffix(key string, now time.Time, ttl time.Duration) string

AppendTTLSuffix appends a time-bucketed TTL suffix to a cache key. The timestamp is rounded down to the nearest TTL interval so that keys naturally "expire" when the current time crosses into the next bucket. If ttl is zero or negative, the key is returned unchanged.

func RoundTimestampToInterval

func RoundTimestampToInterval(t time.Time, interval time.Duration) time.Time

RoundTimestampToInterval rounds a timestamp down to the nearest interval boundary. If interval is zero or negative, t is returned unchanged. For example, with a 5-minute interval:

  • 10:02:30 → 10:00:00
  • 10:05:00 → 10:05:00
  • 10:07:30 → 10:05:00

func StripTTLSuffix

func StripTTLSuffix(key string) string

StripTTLSuffix removes the trailing ":ttl:<timestamp>" suffix appended by AppendTTLSuffix. It trims only the last marker so that keys whose body happens to contain a literal ":ttl:" substring are preserved intact. If the marker is not present, the key is returned unchanged.

Types

type GroupcacheAdapter

type GroupcacheAdapter[K any, V any] struct {
	// contains filtered or unexported fields
}

GroupcacheAdapter wraps a groupcache group with typed key/value serialization and TTL-bucketed cache keys.

K is the domain key type (e.g. ValidatorInput, *DataSourceInput). V is the domain value type (e.g. *Result, *cachedEntry).

func NewGroupcacheAdapter

func NewGroupcacheAdapter[K any, V any](
	groupName string,
	serializeKey func(K) (string, error),
	deserializeKey func(string) (K, error),
	fetch func(context.Context, K) (V, error),
	serializeValue func(V) ([]byte, error),
	deserializeValue func([]byte) (V, error),
	opts ...GroupcacheAdapterOption,
) (*GroupcacheAdapter[K, V], error)

NewGroupcacheAdapter creates a GroupcacheAdapter.

Required parameters are positional: groupName uniquely identifies the groupcache group within the process, and the serialize/deserialize/fetch functions handle domain-type conversion and origin fetches.

func (*GroupcacheAdapter[K, V]) Get

func (a *GroupcacheAdapter[K, V]) Get(ctx context.Context, key K) (V, error)

Get retrieves a value from the cache, fetching from the source on a miss. The key is serialized, TTL-bucketed, and passed to groupcache.

type GroupcacheAdapterOption

type GroupcacheAdapterOption func(*groupcacheAdapterConfig)

GroupcacheAdapterOption configures optional behavior of a GroupcacheAdapter.

func WithCacheSizeBytes

func WithCacheSizeBytes(n int64) GroupcacheAdapterOption

WithCacheSizeBytes sets the maximum cache size in bytes. Default: 64 MB.

func WithClock

func WithClock(c clock.Clock) GroupcacheAdapterOption

WithClock sets the clock used for TTL bucketing. Default: clock.NewSystemClock.

func WithTTL

func WithTTL(ttl func() TTL) GroupcacheAdapterOption

WithTTL sets the function that returns the time-to-live for cached entries. Called on each Get to allow dynamic TTL (though typically static). If not set, entries are cached indefinitely.

type TTL

type TTL = time.Duration

TTL is the time-to-live for a cache entry.

Jump to

Keyboard shortcuts

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