cached

package
v1.20.3 Latest Latest
Warning

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

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

Documentation

Overview

Package cached implements a cached function runner.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheMissReason

type CacheMissReason string

A CacheMissReason indicates what caused a cache miss.

const (
	ReasonNotCached       CacheMissReason = "NotCached"
	ReasonDeadlineExpired CacheMissReason = "DeadlineExpired"
	ReasonEmptyRequestTag CacheMissReason = "EmptyRequestTag"
	ReasonError           CacheMissReason = "Error"
)

Cache miss reasons.

type FileBackedRunner

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

A FileBackedRunner wraps another function runner. It caches responses returned by that runner to the filesystem. It only caches responses that specify a TTL. Requests are served from cache if there's a cached response for an identical request with an unexpired TTL.

func NewFileBackedRunner

func NewFileBackedRunner(wrap FunctionRunner, path string, o ...FileBackedRunnerOption) *FileBackedRunner

NewFileBackedRunner creates a new function runner that wraps another runner, caching its responses to the filesystem.

func (*FileBackedRunner) CacheFunction

CacheFunction runs a function and caches its response if the TTL is non-zero.

func (*FileBackedRunner) GarbageCollectFiles

func (r *FileBackedRunner) GarbageCollectFiles(ctx context.Context, interval time.Duration)

GarbageCollectFiles runs every interval until the supplied context is cancelled. It garbage collects cached responses with expired deadlines.

func (*FileBackedRunner) GarbageCollectFilesNow

func (r *FileBackedRunner) GarbageCollectFilesNow(ctx context.Context) (int, error)

GarbageCollectFilesNow immediately garbage collects any cached responses with expired deadlines.

func (*FileBackedRunner) RunFunction

RunFunction tries to return a response from cache. It falls back to calling the wrapped runner.

type FileBackedRunnerOption

type FileBackedRunnerOption func(r *FileBackedRunner)

A FileBackedRunnerOption configures a FileBackedRunner.

func WithFilesystem

func WithFilesystem(fs afero.Fs) FileBackedRunnerOption

WithFilesystem specifies which filesystem implementation the FileBackedRunner should use. The runner will ignore its path argument and cache files at the root of this filesystem. Wrap your desired filesystem with afero.BasePathFS to cache files under a specific path.

func WithLogger

WithLogger specifies which logger the FileBackedRunner should use.

func WithMaxTTL

func WithMaxTTL(ttl time.Duration) FileBackedRunnerOption

WithMaxTTL clamps the maximum TTL for cached responses. The maximum TTL will be used to set the cache deadline for any response with a TTL greater than the max.

func WithMetrics

func WithMetrics(m Metrics) FileBackedRunnerOption

WithMetrics specifies which metrics the FileBackedRunner should use.

type FunctionRunner

type FunctionRunner interface {
	// RunFunction runs the named composition function.
	RunFunction(ctx context.Context, name string, req *fnv1.RunFunctionRequest) (*fnv1.RunFunctionResponse, error)
}

A FunctionRunner runs a composition function.

type Metrics

type Metrics interface {
	// Hit records a cache hit.
	Hit(name string)

	// Miss records a cache miss.
	Miss(name string)

	// Error records a cache error.
	Error(name string)

	// Write records a cache write.
	Write(name string)

	// Delete records a cache delete, i.e. due to garbage collection.
	Delete(name string)

	// WroteBytes records bytes written to the cache.
	WroteBytes(name string, b int)

	// DeletedBytes records bytes deleted from the cache.
	DeletedBytes(name string, b int)

	// ReadDuration records the time taken by a cache hit.
	ReadDuration(name string, d time.Duration)

	// WriteDuration records the time taken to write to cache.
	WriteDuration(name string, d time.Duration)
}

Metrics for the function response cache.

type NopMetrics

type NopMetrics struct{}

NopMetrics does nothing.

func (*NopMetrics) Delete

func (m *NopMetrics) Delete(_ string)

Delete does nothing.

func (*NopMetrics) DeletedBytes

func (m *NopMetrics) DeletedBytes(_ string, _ int)

DeletedBytes does nothing.

func (*NopMetrics) Error

func (m *NopMetrics) Error(_ string)

Error does nothing.

func (*NopMetrics) Hit

func (m *NopMetrics) Hit(_ string)

Hit does nothing.

func (*NopMetrics) Miss

func (m *NopMetrics) Miss(_ string)

Miss does nothing.

func (*NopMetrics) ReadDuration

func (m *NopMetrics) ReadDuration(_ string, _ time.Duration)

ReadDuration does nothing.

func (*NopMetrics) Write

func (m *NopMetrics) Write(_ string)

Write does nothing.

func (*NopMetrics) WriteDuration

func (m *NopMetrics) WriteDuration(_ string, _ time.Duration)

WriteDuration does nothing.

func (*NopMetrics) WroteBytes

func (m *NopMetrics) WroteBytes(_ string, _ int)

WroteBytes does nothing.

type PrometheusMetrics

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

PrometheusMetrics for the function response cache.

func NewPrometheusMetrics

func NewPrometheusMetrics() *PrometheusMetrics

NewPrometheusMetrics exposes function response cache metrics via Prometheus.

func (*PrometheusMetrics) Collect

func (m *PrometheusMetrics) Collect(ch chan<- prometheus.Metric)

Collect is called by the Prometheus registry when collecting metrics. The implementation sends each collected metric via the provided channel and returns once the last metric has been sent.

func (*PrometheusMetrics) Delete

func (m *PrometheusMetrics) Delete(name string)

Delete records a cache delete, i.e. due to garbage collection.

func (*PrometheusMetrics) DeletedBytes

func (m *PrometheusMetrics) DeletedBytes(name string, b int)

DeletedBytes records bytes deleted from the cache.

func (*PrometheusMetrics) Describe

func (m *PrometheusMetrics) Describe(ch chan<- *prometheus.Desc)

Describe sends the super-set of all possible descriptors of metrics collected by this Collector to the provided channel and returns once the last descriptor has been sent.

func (*PrometheusMetrics) Error

func (m *PrometheusMetrics) Error(name string)

Error records a cache error.

func (*PrometheusMetrics) Hit

func (m *PrometheusMetrics) Hit(name string)

Hit records a cache hit.

func (*PrometheusMetrics) Miss

func (m *PrometheusMetrics) Miss(name string)

Miss records a cache miss.

func (*PrometheusMetrics) ReadDuration

func (m *PrometheusMetrics) ReadDuration(name string, d time.Duration)

ReadDuration records the time taken by a cache hit.

func (*PrometheusMetrics) Write

func (m *PrometheusMetrics) Write(name string)

Write records a cache write.

func (*PrometheusMetrics) WriteDuration

func (m *PrometheusMetrics) WriteDuration(name string, d time.Duration)

WriteDuration records the time taken to write to cache.

func (*PrometheusMetrics) WroteBytes

func (m *PrometheusMetrics) WroteBytes(name string, b int)

WroteBytes records bytes written from the cache.

Directories

Path Synopsis
proto

Jump to

Keyboard shortcuts

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