simple

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2024 License: Apache-2.0, BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package simple provides the Simple Cache API, a simplified interface to inserting and retrieving entries from Fastly's cache.

Cache operations are local to the Fastly POP serving the request. Purging can also be performed globally.

For more advanced uses, see the Core Cache API in the github.com/fastly/compute-sdk-go/cache/core package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key []byte) (io.ReadCloser, error)

Get retrieves the object stored in the cache for the given key. If the key is not cached, core.ErrNotFound is returned. Keys can be up to 4096 bytes in length.

The returned io.ReadCloser must be closed by the caller when finished.

func GetOrSet

func GetOrSet(key []byte, setFn func() (CacheEntry, error)) (io.ReadCloser, error)

GetOrSet retrieves the object stored in the cache for the given key if it exists, or inserts and returns the contents by running the provided setFn function.

The setFn function is only run when no value is present for the key, and no other client is in the process of setting it. The function should return a populated CacheEntry or an error.

If the setFn function returns an error, nothing will be saved to the cache and the error will be returned from the GetOrSet function. Other concurrent readers will also see an error while reading.

The returned io.ReadCloser must be closed by the caller when finished.

func GetOrSetEntry

func GetOrSetEntry(key []byte, entry CacheEntry) (io.ReadCloser, error)

GetOrSetEntry retrieves the object stored in the cache for the given key if it exists, or inserts and returns the contents provided in the CacheEntry.

The cache entry is only inserted when no value is present for the key, and no other client is in the process of setting it.

If the cache entry body content is costly to compute, consider using GetOrSet instead to avoid creating its io.Reader in the case where the value is already present.

The returned io.ReadCloser must be closed by the caller when finished.

func Purge

func Purge(key []byte, opts PurgeOptions) error

Purge removes the entry associated with the given cache key, if one exists.

The scope of the purge can be controlled with the PurgeOptions.

Purges are handled asynchronously, and the cached object may persist in cache for a short time (~150ms or less) after this function returns.

func SurrogateKeyForCacheKey

func SurrogateKeyForCacheKey(cacheKey []byte, scope PurgeScope) string

SurrogateKeyForCacheKey creates a surrogate key for the given cache key and purge scope that is compatible with the Simple Cache API. Each cache entry for the Simple Cache API is configured with surrogate keys from this function.

This function is provided as a convenience for implementors wishing to add a Simple Cache-compatible surrogate key manually via the Core Cache API (github.com/fastly/compute-sdk-go/cache/core) for interoperability with Purge.

Types

type CacheEntry

type CacheEntry struct {
	// The contents of the cached object.
	Body io.Reader

	// The time-to-live for the cached object.
	TTL time.Duration
}

CacheEntry contains the contents and TTL (time-to-live) for an item to be added to the cache via GetOrSet or [GetOrSetContents].

type PurgeOptions

type PurgeOptions struct {
	Scope PurgeScope
}

PurgeOptions controls the behavior of the Purge function.

type PurgeScope

type PurgeScope uint32

PurgeScope controls the scope of a purge operation. It is used in the PurgeOptions struct.

const (
	// PurgeScopePOP purges the entry only from the local POP cache.
	PurgeScopePOP PurgeScope = iota
	// PurgeScopeGlobal purges the entry from all POP caches.
	PurgeScopeGlobal
)