cacheable

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithCacheConfig

func ContextWithCacheConfig(ctx context.Context, config CacheConfig) context.Context

ContextWithCacheConfig Returns a context with a CacheConfig. The cache config can be used to override the default ttl or to provide a custom cache key.

func GenerateKeyHash

func GenerateKeyHash(r *http.Request) string

GenerateKeyHash A generic method for creating a string cache key from an http.Request object

func StatusCodeValidator

func StatusCodeValidator(r *http.Response) bool

StatusCodeValidator a simple Validator function that will return true if the http.Response status code is in the success range.

Types

type CacheConfig

type CacheConfig struct {
	Key        string
	TTLSeconds int
}

CacheConfig is an optional configuration that can be passed via context to alter caching behavior on a per request basis

type Client

type Client interface {
	Do(req *http.Request) (*http.Response, error)
}

type ClientFunc

type ClientFunc func(req *http.Request) (*http.Response, error)

func (ClientFunc) Do

func (c ClientFunc) Do(req *http.Request) (*http.Response, error)

type HTTPCacheProvider

type HTTPCacheProvider interface {
	Get(string) (*http.Response, bool)
	Set(string, *http.Response, time.Duration)
}

HTTPCacheProvider is a cache interface that is used to cache http responses

type Middleware

type Middleware func(client Client) Client

func NewCacheableMiddleware

func NewCacheableMiddleware(c HTTPCacheProvider, ttlSeconds int, isValid Validator) Middleware

NewCacheableMiddleware Given a HTTPCacheProvider, TTL in seconds, and a validator function will created a Middleware that can be used to create cache enabled HTTP clients. TTL is not enforced by cacheable middleware and must be enforced by the HTTPCacheProvider. Cacheable middleware will not store an HTTP response that does not return true when passed to the Validator Function.

type Validator

type Validator func(*http.Response) bool

Validator user defined function that should return true if the response should be cached. See StatusCodeValidator

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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