cache

package
v1.0.46 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cache provides a generalized cache package that allows anything to be cached without having to know underlying details of where that cache is stored. All items are serialized into JSON and stored as strings. When retrieved, it's unmarshaled into the provided container. A single cache is maintained at the package level. The provided cacher should be thread-safe, as no locking occurs in this package.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrCacheNil is returned when caching falied due to the provided Cacher being nil
	ErrCacheNil = errors.New("cache is nil")

	// ErrCacheDisabled is returned when caching falied due to caching being disabled
	ErrCacheDisabled = errors.New("cache is disabled")

	// ErrDeserialize is returned when cache extraction failed due to deserialization errors.
	ErrDeserialize = errors.New("cached value could not be deserialized")

	// ErrNotFound is returned when no data was found
	ErrNotFound = errors.New("not found")

	// Enabled allows caching to be turned on and off. This is useful for turning cache off
	// via environment variables.
	Enabled = true
)

Functions

func Get added in v1.0.17

func Get(r metrics.Recorder, key string, container interface{}) error

Get a value from cache.

func HandlerWrapper added in v1.0.17

func HandlerWrapper(cacheDuration int, next http.Handler) http.HandlerFunc

HandlerWrapper provices a cache-layer wrapper for a single API route.

func Initialize added in v1.0.26

func Initialize(cache Cacher, defaultDuration time.Duration)

Initialize must be called prior to use. Do this in main.

func Middleware added in v1.0.29

func Middleware(cacheDuration int, excludedPaths []string) func(http.Handler) http.Handler

Middleware provides a cache-layer middleware for caching the input/output for GET requests.

func Set added in v1.0.17

func Set(m metrics.Recorder, key string, value interface{}) error

Set a value in cache. Value MUST be serializeable by JSON. UnExported fields will be ignored!

func SetWithDuration added in v1.0.17

func SetWithDuration(m metrics.Recorder, key string, value interface{}, d time.Duration) error

SetWithDuration sets a value in cache. Value MUST be serializeable by JSON. UnExported fields will be ignored!

Types

type Cacher added in v1.0.26

type Cacher interface {
	Get(metrics.Recorder, string) (interface{}, error)
	Set(metrics.Recorder, string, interface{}) error
	SetWithDuration(metrics.Recorder, string, interface{}, time.Duration) error
	Delete(metrics.Recorder, string) error
}

Cacher provides an interface for working with a cache store.

func NewMemoryCache added in v1.0.26

func NewMemoryCache(defaultTTL time.Duration) Cacher

NewMemoryCache returns an in-memory Cacher

type MemoryCache added in v1.0.26

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

MemoryCache is an in-memory cache storage that satisfies the Cacher interface.

func (*MemoryCache) Delete added in v1.0.26

func (c *MemoryCache) Delete(m metrics.Recorder, key string) error

Delete removes a key from cache.

func (*MemoryCache) Get added in v1.0.26

func (c *MemoryCache) Get(m metrics.Recorder, key string) (interface{}, error)

Get a value from cache.

func (*MemoryCache) Set added in v1.0.26

func (c *MemoryCache) Set(m metrics.Recorder, key string, value interface{}) error

Set a value in cache.

func (*MemoryCache) SetWithDuration added in v1.0.26

func (c *MemoryCache) SetWithDuration(m metrics.Recorder, key string, value interface{}, d time.Duration) error

SetWithDuration sets a value in cache.

type ResponseWriterTee

type ResponseWriterTee struct {
	Buffer     bytes.Buffer
	StatusCode int
	// contains filtered or unexported fields
}

ResponseWriterTee captures input to an http.ResponseWriter

func (*ResponseWriterTee) Header

func (w *ResponseWriterTee) Header() http.Header

Header proxies http.ResponseWriter Header

func (*ResponseWriterTee) Write

func (w *ResponseWriterTee) Write(b []byte) (int, error)

Write proxies http.ResponseWriter Write

func (*ResponseWriterTee) WriteHeader

func (w *ResponseWriterTee) WriteHeader(statusCode int)

WriteHeader proxies http.ResponseWriter WriteHeader

Jump to

Keyboard shortcuts

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