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 ¶
- Variables
- func Get(r metrics.Recorder, key string, container interface{}) error
- func HandlerWrapper(cacheDuration int, next http.Handler) http.HandlerFunc
- func Initialize(cache Cacher, defaultDuration time.Duration)
- func Middleware(cacheDuration int, excludedPaths []string) func(http.Handler) http.Handler
- func Set(m metrics.Recorder, key string, value interface{}) error
- func SetWithDuration(m metrics.Recorder, key string, value interface{}, d time.Duration) error
- type Cacher
- type MemoryCache
- func (c *MemoryCache) Delete(m metrics.Recorder, key string) error
- func (c *MemoryCache) Get(m metrics.Recorder, key string) (interface{}, error)
- func (c *MemoryCache) Set(m metrics.Recorder, key string, value interface{}) error
- func (c *MemoryCache) SetWithDuration(m metrics.Recorder, key string, value interface{}, d time.Duration) error
- type ResponseWriterTee
Constants ¶
This section is empty.
Variables ¶
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 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
Initialize must be called prior to use. Do this in main.
func Middleware ¶ added in v1.0.29
Middleware provides a cache-layer middleware for caching the input/output for GET requests.
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
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