cache

package module
v0.0.0-...-875a7fa Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2017 License: MIT Imports: 14 Imported by: 4

README

cacher

=========

[WIP]

Summary

package cacher provides a http.RoundTripper implementation that works as a mostly RFC-compliant cache for http responses.

It is only suitable for use as a 'private' cache (i.e. for a web-browser or an API-client and not for a shared proxy).

Cache Backends


Defaults
  • The built-in 'memory' cache stores responses in an in-memory map.
Memory
File System - Storage
Local
Cloud
KV
RDB
  • plugins/gorm provides gorm implementations, mainly for debugging and development

Getting started

Below is a basic example of usage.

func httpCacheExample() {
    numOfRequests := 0
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Cache-Control", fmt.Sprintf("private, max-age=10"))
        if numOfRequests == 0 {
            w.Write([]byte("Hello!"))
        } else {
            w.Write([]byte("Goodbye!"))
        }
        numOfRequests++
    }))

    httpClient := &http.Client{
        Transport: httpcache.NewMemoryCacheTransport(),
    }
    makeRequest(ts, httpClient) // "Hello!"

    // The second request is under max-age, so the cache is used rather than hitting the server
    makeRequest(ts, httpClient) // "Hello!"

    // Sleep so the max-age is passed
    time.Sleep(time.Second * 11)

    makeRequest(ts, httpClient) // "Goodbye!"
}

func makeRequest(ts *httptest.Server, httpClient *http.Client) {
    resp, _ := httpClient.Get(ts.URL)
    var buf bytes.Buffer
    io.Copy(&buf, resp.Body)
    println(buf.String())
}

License

Documentation

Overview

package cacher provides a http.RoundTripper implementation that works as a mostly RFC-compliant cache for http responses.

It is only suitable for use as a 'private' cache (i.e. for a web-browser or an API-client and not for a shared proxy).

Index

Constants

View Source
const (
	XFromCache = "X-From-Cache" // XFromCache is the header added to responses that are returned from the cache

)

Variables

View Source
var (
	RequestBufferSize  int = 4096
	ResponseBufferSize int = 4096
	Verbose            bool
	Debug              bool
)
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 InitLogger

func InitLogger(backend logger.Logger) (bool, error)

func NewBlockingTransport

func NewBlockingTransport(rt http.RoundTripper) http.RoundTripper

func NewLogger

func NewLogger() (logx logger.Logger, err error)

func NewTimeoutClient

func NewTimeoutClient(connectTimeout time.Duration, readWriteTimeout time.Duration) *http.Client

func TimeoutDialer

func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error)

Types

type BlockingTransport

type BlockingTransport struct {
	Transport http.RoundTripper
	// contains filtered or unexported fields
}

func (*BlockingTransport) RoundTrip

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

type Cache

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

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

type CustomTransport

type CustomTransport struct {
	Transport http.RoundTripper
	MaxStale  int //seconds
	Debug     bool
}

func (*CustomTransport) RoundTrip

func (c *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error)

type ETagTransport

type ETagTransport struct {
	ETag string
}

func (*ETagTransport) RoundTrip

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

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) Action

func (c *MemoryCache) Action(name string, args ...interface{}) (map[string]*interface{}, error)

func (*MemoryCache) Debug

func (c *MemoryCache) Debug(action string)

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(key string)

Delete removes key from the cache

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 cache with key

type NotFound

type NotFound struct{}

func (*NotFound) Action

func (c *NotFound) Action(name string, args ...interface{}) (map[string]*interface{}, error)

func (*NotFound) Debug

func (c *NotFound) Debug(action string)

func (*NotFound) Delete

func (c *NotFound) Delete(string)

func (*NotFound) Get

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

func (*NotFound) Set

func (c *NotFound) Set(string, []byte)

type Transport

type Transport struct {
	// The RoundTripper interface actually used to make requests
	// If nil, http.DefaultTransport is used
	// httpstats.NewTransport(t)
	Transport http.RoundTripper
	Cache     Cache
	// If true, responses returned from the cache will be given an extra header, X-From-Cache
	MarkCachedResponses bool
	OnDuplicateAbort    bool
	Stats               bool
	StatsEngine         string
	StatsEndpoint       string
	Debug               bool
	Timeout             time.Duration
	Dial                time.Duration
	// contains filtered or unexported fields
}

Transport is an implementation of http.RoundTripper that will return values from a cache 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 cache implementation

func NewTransport

func NewTransport(c Cache) *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) Info

func (t *Transport) Info()

func (*Transport) RoundTrip

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

RoundTrip takes a Request and returns a Response

If there is a fresh Response already in cache, 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.

Directories

Path Synopsis
backend
cloud/azurestorage
Package azurestorage provides an implementation of httpcache.Cache that stores and retrieves data using Azure Storage.
Package azurestorage provides an implementation of httpcache.Cache that stores and retrieves data using Azure Storage.
cloud/diskvs3/s3cache
Package s3cache provides an httpcache.Cache implementation that stores cached values on Amazon S3.
Package s3cache provides an httpcache.Cache implementation that stores cached values on Amazon S3.
local/boltdb/lru
This package provides a simple LRU cache backed by boltdb It is based on the LRU implementation in groupcache: https://github.com/golang/groupcache/tree/master/lru
This package provides a simple LRU cache backed by boltdb It is based on the LRU implementation in groupcache: https://github.com/golang/groupcache/tree/master/lru
local/diskv
Package diskcache provides an implementation of httpcache.Cache that uses the diskv package to supplement an in-memory map with persistent storage
Package diskcache provides an implementation of httpcache.Cache that uses the diskv package to supplement an in-memory map with persistent storage
local/forward/consistenthash
Package consistenthash provides an implementation of a ring hash.
Package consistenthash provides an implementation of a ring hash.
local/forward/lru
Package lru provides an lru cache algorithm over an existing cache.
Package lru provides an lru cache algorithm over an existing cache.
local/lru
Package lrucache provides a byte-size-limited implementation of httpcache.Cache that stores data in memory.
Package lrucache provides a byte-size-limited implementation of httpcache.Cache that stores data in memory.
local/lru/twotier
Package twotier provides a wrapper for two httpcache.Cache instances, allowing you to use both a small and fast cache for popular objects and fall back to a larger and slower cache for less popular ones.
Package twotier provides a wrapper for two httpcache.Cache instances, allowing you to use both a small and fast cache for popular objects and fall back to a larger and slower cache for less popular ones.
remote/leveldb
Package leveldbcache provides an implementation of httpcache.Cache that uses github.com/syndtr/goleveldb/leveldb
Package leveldbcache provides an implementation of httpcache.Cache that uses github.com/syndtr/goleveldb/leveldb
remote/memcache
Package memcache provides an implementation of httpcache.Cache that uses gomemcache to store cached responses.
Package memcache provides an implementation of httpcache.Cache that uses gomemcache to store cached responses.
remote/redis
Package rediscache provides an implementation of httpcache.Cache that stores and retrieves data using Redis.
Package rediscache provides an implementation of httpcache.Cache that stores and retrieves data using Redis.
remote/redis/redigo
Package redis provides a redis interface for http caching.
Package redis provides a redis interface for http caching.
remote/seaweedfs
Package seaweedfscache provides an implementation of httpcache.Cache that stores and retrieves data using Seaweedfs.
Package seaweedfscache provides an implementation of httpcache.Cache that stores and retrieves data using Seaweedfs.
examples
plugins

Jump to

Keyboard shortcuts

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