cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides wrappers for better interacting with the underlying cache implementation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheHandlerWrapper

func CacheHandlerWrapper(
	cacheClient Client,
	cacheableEntity CacheableEntity,
	requestHandlerFunc CacheableRequestHandler,
	preRequestFunc PreRequestFunc,
	postRequestFunc PostRequestFunc,
) func(c emf.Context) error

CacheHandlerWrapper handles wrapping typical echo request handlers to provide request based caching and cache-breaking. The CachebleEntity provides the functionality for generating the cache key based on the request. The CacheableRequestHandler is a request handler formatted to respond in to the cache handler wrapper. preRequestFunc and postRequestFunc are called before and after the request respectively when provided.

func DefaultCacheHandlerWrapper

func DefaultCacheHandlerWrapper(
	cacheClient Client,
	cacheableEntity CacheableEntity,
	requestHandlerFunc CacheableRequestHandler,
) func(c emf.Context) (err error)

DefaultCacheHandlerWrapper handles caching and cache-breaking intended to work across GET, PUT & POST action endpoints we support.

func DefaultPostRequestFunc

func DefaultPostRequestFunc(c emf.Context, ctx Client, ce CacheableEntity, cr CacheResponse) (err error)

DefaultPostRequestFunc invalidates entries in the cache based on the CacheableEntity

func DefaultPreRequestFunc

func DefaultPreRequestFunc(c emf.Context, ctx Client, ce CacheableEntity) ([]byte, error)

DefaultPreRequestFunc requests entity from the cached based on the CacheableEntity

func GetBaseCacheKey

func GetBaseCacheKey(c emf.Context) string

GetBaseCacheKey returns the base routed URL to be used for a more general cache key.

func GetRoleBasedCacheKey

func GetRoleBasedCacheKey(c emf.Context) string

GetRoleBasedCacheKey returns the cache path for URL with role May result in prefixing the cache key with pds, tp, sp, or pa whom may have different views of the requested entity.

Types

type CacheResponse

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

CacheResponse is the response needed by handlers using the CacheWrapper.

func NewCacheResponse

func NewCacheResponse(r interface{}, code int) *CacheResponse

NewCacheResponse returns a new CacheResponse

func NewCacheResponseOK

func NewCacheResponseOK(r interface{}) *CacheResponse

NewCacheResponseOK returns a new CacheResponse

func NewEmptyCacheResponse

func NewEmptyCacheResponse(code int) *CacheResponse

NewEmptyCacheResponse returns a new CacheResponse

func (*CacheResponse) AddIDToInvalidate

func (c *CacheResponse) AddIDToInvalidate(id string)

AddIDToInvalidate adds the id to the list of ids to be invalidated.

func (CacheResponse) GetResponse

func (c CacheResponse) GetResponse() interface{}

GetResponse gets the response

func (CacheResponse) GetResponseCode

func (c CacheResponse) GetResponseCode() int

GetResponseCode gets the http response code

func (*CacheResponse) SetResponse

func (c *CacheResponse) SetResponse(r interface{})

SetResponse sets response to supplied interface

func (*CacheResponse) SetResponseCode

func (c *CacheResponse) SetResponseCode(code int)

SetResponseCode sets the http response code

type CacheableEntity

type CacheableEntity interface {
	GetCacheKey(c emf.Context) string
	GetCacheKeysFromEntityID(entityID string) []string
}

CacheableEntity interface must be impemented for response objects that use the CacheWrapper.

type CacheableRequestHandler

type CacheableRequestHandler func(emf.Context) (*CacheResponse, error)

CacheableRequestHandler is a request handler returning a CacheResponse

type Client

type Client interface {
	Set(path string, data []byte) error
	SetEx(path string, data []byte) error
	Get(path string) ([]byte, bool, error)
	Delete(path string) error
}

Client is the interface for any application level cache implementation.

type DefaultCacheableEntity

type DefaultCacheableEntity struct{}

DefaultCacheableEntity can be embedded into a struct so that it fulfills the CacheableEntity interface using the default behavior.

func (*DefaultCacheableEntity) GetCacheKey

func (d *DefaultCacheableEntity) GetCacheKey(c emf.Context) string

GetCacheKey satisfies the default CacheableEntity.

func (*DefaultCacheableEntity) GetCacheKeysFromEntityID

func (d *DefaultCacheableEntity) GetCacheKeysFromEntityID(id string) []string

GetCacheKeysFromEntityID satisfies the default CacheableEntity.

type PostRequestFunc

type PostRequestFunc func(c emf.Context, ctx Client, ce CacheableEntity, cr CacheResponse) error

PostRequestFunc can be used after the reequest handler is called

type PreRequestFunc

type PreRequestFunc func(c emf.Context, ctx Client, ce CacheableEntity) ([]byte, error)

PreRequestFunc can be used before the request handler is called

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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