caching

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2020 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package twotier provides a wrapper for two httpcache.Cache instances, allowing you to use both a small and fast RedisHTTPCache for popular objects and fall back to a larger and slower RedisHTTPCache for less popular ones.

Index

Constants

View Source
const (

	// XFromCache is the header added to responses that are returned from the RedisHTTPCache
	XFromCache = "X-Cache"
)

Variables

View Source
var ErrNoDateHeader = errors.New("no Date header")

ErrNoDateHeader indicates that the HTTP headers contained no Date header.

Functions

func CachedResponse

func CachedResponse(c Cache, req *http.Request) (resp *http.Response, err error)

CachedResponse returns the cached http.Response for req if present, and nil otherwise.

func Date

func Date(respHeaders http.Header) (date time.Time, err error)

Date parses and returns the value of the Date header.

func NewRedisPool

func NewRedisPool(net, host, port, password string, usetls bool, logger *zap.Logger) *redis.Pool

Types

type BadgerCache

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

func NewBadgerCache

func NewBadgerCache(db *badger.DB, uniq string, logger *zap.Logger) *BadgerCache

func (*BadgerCache) Delete

func (c *BadgerCache) Delete(key string)

Delete removes the response with key from the BadgerCache.

func (*BadgerCache) Get

func (c *BadgerCache) Get(key string) (resp []byte, ok bool)

Get returns the response corresponding to key if present.

func (*BadgerCache) Set

func (c *BadgerCache) Set(key string, resp []byte)

Set saves a response to the BadgerCache as key.

type Cache

type Cache interface {
	// Get returns the []byte representation of a cached response and a bool
	// set to true if the value isn't empty
	Get(key string) (responseBytes []byte, ok bool)
	// Set stores the []byte representation of a response against a key
	Set(key string, responseBytes []byte)
	// Delete removes the value associated with the key
	Delete(key string)
}

A Cache interface is used by the Transport to store and retrieve responses.

type DoubleCache

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

func NewDoubleCache

func NewDoubleCache(first, second Cache) *DoubleCache

New creates a DoubleCache. Both first and second must be non-nil.

func NewRCache

func NewRCache(pool *redis.Pool, uniq string, memorysize int64, logger *zap.Logger) *DoubleCache

NewWithClient returns a new Cache with the given redis connection.

func (*DoubleCache) Delete

func (c *DoubleCache) Delete(key string)

Delete removes the value associated with a key from both the first and second tier caches.

func (*DoubleCache) Get

func (c *DoubleCache) Get(key string) ([]byte, bool)

Get returns the []byte representation of a cached response and a bool set to true if the key was found. It tries the first tier RedisHTTPCache, and if that's not successful, copies the result from the second tier into the first tier.

func (*DoubleCache) Set

func (c *DoubleCache) Set(key string, value []byte)

Set stores the []byte representation of a response for a given key into the second tier RedisHTTPCache, and deletes the RedisHTTPCache entry from the first tier RedisHTTPCache.

func (*DoubleCache) Size

func (c *DoubleCache) Size() int64

type MemoryCache

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

MemoryCache is an implemtation of Cache that stores responses in an in-memory map.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache returns a new Cache that will store items in an in-memory map

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(key string)

Delete removes key from the RedisHTTPCache

func (*MemoryCache) Get

func (c *MemoryCache) Get(key string) (resp []byte, ok bool)

Get returns the []byte representation of the response and true if present, false if not

func (*MemoryCache) Set

func (c *MemoryCache) Set(key string, resp []byte)

Set saves response resp to the RedisHTTPCache with key

type RedisHTTPCache

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

func NewROnlyCache

func NewROnlyCache(pool *redis.Pool, uniq string, logger *zap.Logger) *RedisHTTPCache

func (RedisHTTPCache) Delete

func (c RedisHTTPCache) Delete(key string)

Delete removes the response with key from the RedisHTTPCache.

func (RedisHTTPCache) Get

func (c RedisHTTPCache) Get(key string) (resp []byte, ok bool)

Get returns the response corresponding to key if present.

func (RedisHTTPCache) Set

func (c RedisHTTPCache) Set(key string, resp []byte)

Set saves a response to the RedisHTTPCache as key.

type Transport

type Transport struct {
	// The RoundTripper interface actually used to make requests
	// If nil, http.DefaultTransport is used
	Transport http.RoundTripper
	Cache     Cache
	// If true, responses returned from the RedisHTTPCache will be given an extra header, X-From-Cache
	MarkCachedResponses bool
	// contains filtered or unexported fields
}

Transport is an implementation of http.RoundTripper that will return values from a RedisHTTPCache where possible (avoiding a network request) and will additionally add validators (etag/if-modified-since) to repeated requests allowing servers to return 304 / Not Modified

func NewMemoryCacheTransport

func NewMemoryCacheTransport() *Transport

NewMemoryCacheTransport returns a new Transport using the in-memory RedisHTTPCache implementation

func NewTransport

func NewTransport(c Cache, logger *zap.Logger) *Transport

NewTransport returns a new Transport with the provided Cache implementation and MarkCachedResponses set to true

func (*Transport) Client

func (t *Transport) Client() *http.Client

Client returns an *http.Client that caches responses.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip takes a Request and returns a Response

If there is a fresh Response already in RedisHTTPCache, then it will be returned without connecting to the server.

If there is a stale Response, then any validators it contains will be set on the new request to give the server a chance to respond with NotModified. If this happens, then the cached Response will be returned.

Jump to

Keyboard shortcuts

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