cache

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package cache provides a size-bounded, LRU-evicting disk cache that implements the httpcache.Cache interface. Responses are stored under a per-hostname subdirectory so they can be cleared by API name.

Index

Constants

View Source
const DefaultMaxBytes = 100 * 1024 * 1024

DefaultMaxBytes is the default maximum cache size (100 MiB).

Variables

This section is empty.

Functions

This section is empty.

Types

type Breakdown

type Breakdown struct {
	Name        string
	SizeBytes   int64
	EntryCount  int
	OldestEntry time.Time
}

Breakdown summarizes a cache slice, such as one host or cache namespace.

type DiskCache

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

DiskCache is a file-system backed HTTP response cache. It satisfies the httpcache.Cache interface (Get/Set/Delete) and additionally provides Info and Clear for the "restish cache" subcommands.

func New

func New(dir string, maxBytes int64, namespace string) (*DiskCache, error)

New returns a DiskCache rooted at dir with the given size cap. dir is created if it does not exist.

func NewWithLogger

func NewWithLogger(dir string, maxBytes int64, namespace string, logger io.Writer) (*DiskCache, error)

NewWithLogger returns a DiskCache that writes non-fatal cache diagnostics to logger when provided.

func (*DiskCache) Clear

func (c *DiskCache) Clear(host string) error

Clear deletes HTTP response cache entries. If host is non-empty, only entries stored under the <host> subdirectory are removed (i.e. responses for one API).

func (*DiskCache) ClearNamespacePrefix

func (c *DiskCache) ClearNamespacePrefix(prefix string) (int, error)

ClearNamespacePrefix deletes entries for every namespace with prefix and returns the number of namespace directories removed.

func (*DiskCache) ClearNamespaces

func (c *DiskCache) ClearNamespaces(namespaces []string) error

ClearNamespaces deletes entries for the given cache namespaces across all hosts. It is used for API-specific clearing because one registered API can have multiple profile base URLs or operation hosts that share a hostname with other APIs.

func (*DiskCache) Delete

func (c *DiskCache) Delete(key string)

Delete removes the cached entry for key.

func (*DiskCache) Get

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

Get returns the cached bytes for key, if present, and updates the file's mtime (used as LRU timestamp).

func (*DiskCache) Info

func (c *DiskCache) Info() (*Info, error)

Info returns current cache statistics.

func (*DiskCache) Set

func (c *DiskCache) Set(key string, data []byte)

Set writes data to disk for key and evicts LRU entries if the total cache size exceeds the configured limit.

func (*DiskCache) WaitEvict

func (c *DiskCache) WaitEvict()

WaitEvict blocks until any pending background eviction goroutine has finished. It is intended for use in tests only.

type Info

type Info struct {
	SizeBytes   int64
	EntryCount  int
	OldestEntry time.Time
	Hosts       []Breakdown
	Namespaces  []Breakdown
}

Info holds statistics about the cache.

Jump to

Keyboard shortcuts

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