cache

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cache provides a way to define caching behavior used in a data pipeline.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run[S, T, K, V any](ctx context.Context, sp Spec[S, T, K, V], arg S, fn CacheableFn[S, T]) (T, error)

Run the passed CacheableFn with caching.

The function's result is cached depending on the passed Spec and its argument.

Types

type CacheableFn

type CacheableFn[S, T any] func(context.Context, S) (T, error)

A signature of a function that the caching is applicable.

type KeyFn

type KeyFn[S, K any] func(S) (*StoreKey[K], error)

A function that defines a corresponding key for an argument.

The return value should be a StoreKey that wraps a key.

type Spec

type Spec[S, T, K, V any] interface {
	// Store that should be used as a cache store
	store.Store[K, V]

	// A function that converts an argument into a cache key.
	Key(S) (*StoreKey[K], error)

	// A function that encodes a cacheable function's result into a value that will be stored in a cache store.
	Encode(T) (V, error)

	// A function that decodes a stored value in a cache store into a cacheable function's result.
	Decode(V) (T, error)
}

Specification of a cache behavior.

func NewMarshalSpec

func NewMarshalSpec[S, T, K any](
	cs store.Store[K, []byte],
	keyFn KeyFn[S, K],
	valueSpec marshal.Spec[T],
) Spec[S, T, K, []byte]

Create a cache spec that uses marshaling to store a function's result.

With this cache spec, any values will be stored as bytes data.

func NewRawSpec

func NewRawSpec[S, T, K any](cs store.Store[K, T], keyFn KeyFn[S, K]) Spec[S, T, K, T]

Create a cache spec that stores a function's result without any conversion.

This spec is typically used, for example, when caching values in memory.

type StoreKey

type StoreKey[K any] struct {
	// contains filtered or unexported fields
}

A key for a cache store.

This struct wraps a key and defines a behavior for the argument, like a normal cache behavior, a write-only behavior, or a bypass behavior.

func Bypass

func Bypass[K any]() *StoreKey[K]

This returns a StoreKey that expects a bypass cache behavior.

With this key, the expected behavior is:

When a cached result does not exist, call a cacheable function, and return the result of the function.
When a cached result exists, call a cacheable function, and return the result of the function.

func IdentityKey

func IdentityKey[S any](el S) (*StoreKey[S], error)

A KeyFn that uses the argument as a key without any conversion.

The expected behavior is a normal cache behavior.

func Key

func Key[K any](v K) *StoreKey[K]

This returns a StoreKey that expects a normal cache behavior.

With this key, the expected behavior is:

When a cached result does not exist, call a cacheable function, store the result, and return it.
When a cached result exists, return the cached value.

func WriteOnlyKey

func WriteOnlyKey[K any](v K) *StoreKey[K]

This returns a StoreKey that expects a write-only cache behavior.

With this key, the expected behavior is:

When a cached result does not exist, call a cacheable function, store the result, and return it.
When a cached result exists, call a cacheable function, overwrite the existing cache with the result, and return it.

Directories

Path Synopsis
internal
Package defines Store interface for cache.
Package defines Store interface for cache.

Jump to

Keyboard shortcuts

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