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

ttlcache

package
v0.244.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ttlcache is a small on-disk key/value cache with per-entry TTLs. Built for the consumer after the lockdown inversion: every aws / kubectl / gh

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Dir string
}

Cache is a directory of TTL'd JSON entries on disk. One file per key, named by sha256(key) so disk layout stays bounded and predictable. The

func New

func New(dir string) *Cache

New returns a Cache rooted at dir. The directory is created lazily on first Set, with mode 0o700 so other local users cannot read cached

func (*Cache) Get

func (c *Cache) Get(key string) ([]byte, bool)

Get returns the cached value for key, or (nil, false) if there is no fresh entry. Any read / unmarshal / TTL failure is reported as a miss

func (*Cache) GetMaxAge

func (c *Cache) GetMaxAge(key string, maxAge time.Duration) ([]byte, bool)

GetMaxAge returns the cached value for key only if a fresh entry exists and its age is within max. Age is measured against the entry's

func (*Cache) GetOrSet

func (c *Cache) GetOrSet(key string, ttl time.Duration, fetch func() ([]byte, error)) ([]byte, error)

GetOrSet returns the cached value for key, or calls fetch() and stores the result if there is no fresh entry. fetch is called at most once.

func (*Cache) Invalidate

func (c *Cache) Invalidate(key string) error

Invalidate removes the entry for key. Returns nil if the entry did not exist (idempotent), the filesystem error otherwise. Used by callers

func (*Cache) Set

func (c *Cache) Set(key string, value []byte, ttl time.Duration) error

Set writes value under key with the given TTL. Returns the underlying filesystem error if the write fails; callers can ignore it (the next

Jump to

Keyboard shortcuts

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