negronicache

package module
v0.0.0-...-bce1e4e Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: Apache-2.0 Imports: 22 Imported by: 0

README

negroni-cache

Go Report Card Build Status Coverage Status

A standard compatible(RFC 7234) HTTP Cache middleware for negroni.

Usage

package main

import (
    "fmt"
    "net/http"

    "github.com/urfave/negroni"
    cah "github.com/trumanw/negroni-cache"
)

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
          fmt.Fprintf(w, "Welcome to the home page!")
    })

    n := negroni.Classic()
    # set expire for 10second
    n.Use(cah.NewMiddleware(cah.NewMemoryCache(), 10))
    n.UseHandler(mux)
    n.Run(":3000")
}

Documentation

Index

Constants

View Source
const (
	CacheHeader     = "X-Cache"
	ProxyDateHeader = "Proxy-Date"
	GRPCStatusOK    = 0
)
View Source
const (
	CacheControlHeader = "Cache-Control"
)

Variables

View Source
var Clock = func() time.Time {
	return time.Now().UTC()
}
View Source
var DebugLogging = true
View Source
var ErrNotFoundInCache = errors.New("Not found in cache")

Returned when a resource doesn't exist

Functions

func CorrectedAge

func CorrectedAge(h http.Header, reqTime, respTime time.Time) (time.Duration, error)

CorrectedAge adjusts the age of a resource for clock skew and travel time

Types

type ByteReadSeekCloser

type ByteReadSeekCloser struct {
	*bytes.Reader
}

func (*ByteReadSeekCloser) Close

func (brsc *ByteReadSeekCloser) Close() error

type Cache

type Cache interface {
	Header(key string) (Header, error)
	Store(res *Resource, keys ...string) error
	Retrieve(key string) (*Resource, error)
	Invalidate(keys ...string)
	Freshen(res *Resource, keys ...string) error
}

func NewDiskCache

func NewDiskCache(dir string) (Cache, error)

NewDiskCache returns a disk-backed cache

func NewMemoryCache

func NewMemoryCache() Cache

NewMemoryCache returns an ephemeral cache in memory

func NewVFSCache

func NewVFSCache(fs vfs.VFS) Cache

NewCache returns a cache backend off the provided VFS

type CacheControl

type CacheControl map[string][]string

func ParseCacheControl

func ParseCacheControl(input string) (CacheControl, error)

func ParseCacheControlHeaders

func ParseCacheControlHeaders(h http.Header) (CacheControl, error)

func (CacheControl) Add

func (cc CacheControl) Add(key, val string)

func (CacheControl) Duration

func (cc CacheControl) Duration(key string) (time.Duration, error)

func (CacheControl) Get

func (cc CacheControl) Get(key string) (string, bool)

func (CacheControl) Has

func (cc CacheControl) Has(key string) bool

func (CacheControl) String

func (cc CacheControl) String() string

type CacheRequest

type CacheRequest struct {
	*http.Request
	Key          Key
	Time         time.Time
	CacheControl CacheControl
}

func NewCacheRequest

func NewCacheRequest(r *http.Request) (*CacheRequest, error)
type Header struct {
	http.Header
	StatusCode int
}

type Key

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

Key represents a unique identifier for a resource in the cache

func NewKey

func NewKey(method string, u *url.URL, h http.Header) Key

NewKey returns a new Key instance

func NewRequestKey

func NewRequestKey(r *http.Request) Key

NewRequestKey generates a Key for a request

func (Key) ForMethod

func (k Key) ForMethod(method string) Key

ForMethod returns a new Key with a given method

func (Key) String

func (k Key) String() string

func (Key) Vary

func (k Key) Vary(varyHeader string, r *http.Request) Key

Vary returns a Key that is varied on particular headers in a http.Request

type Middleware

type Middleware struct {
	Shared bool
	// contains filtered or unexported fields
}

Middleware is the cache middlware for negroni

func NewMiddleware

func NewMiddleware(cache Cache, expiry int32) *Middleware

NewMiddleware retrieves an instance of Cache handler

func (*Middleware) CacheResource

func (ch *Middleware) CacheResource(res *Resource, r *CacheRequest)

CacheResource can store the response in the cache.

func (*Middleware) Freshness

func (ch *Middleware) Freshness(res *Resource, r *CacheRequest) (time.Duration, error)

Freshness returns the duration that a requested resource will be fresh for

func (*Middleware) GenerateCacheKeys

func (ch *Middleware) GenerateCacheKeys(res *Resource, r *CacheRequest) []string

func (*Middleware) InvalidCache

func (ch *Middleware) InvalidCache(rw http.ResponseWriter) bool

InvalidCache force update cache when reaching cache expiry

func (*Middleware) LookupInCached

func (ch *Middleware) LookupInCached(req *CacheRequest) (*Resource, error)

LookupInCached finds the best matching Resource for the request, or nil and ErrNotFoundInCache if none is found

func (*Middleware) ServeHTTP

func (ch *Middleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

func (*Middleware) ServeResource

func (ch *Middleware) ServeResource(res *Resource, rw http.ResponseWriter, req *CacheRequest)

ServeResource is used for wrapping the ResponseWriter and returning the request.

func (*Middleware) UpstreamWithCache

func (ch *Middleware) UpstreamWithCache(rw http.ResponseWriter, r *CacheRequest, next http.HandlerFunc)

UpstreamWithCache returns the request to a specific handler and stores the result

type ReadSeekCloser

type ReadSeekCloser interface {
	io.Reader
	io.Seeker
	io.Closer
}

type Resource

type Resource struct {
	ReadSeekCloser
	RequestTime, ResponseTime time.Time
	// contains filtered or unexported fields
}

func NewResource

func NewResource(statusCode int, body ReadSeekCloser, hdrs http.Header) *Resource

func NewResourceBytes

func NewResourceBytes(statusCode int, b []byte, hdrs http.Header) *Resource

func (*Resource) Age

func (r *Resource) Age() (time.Duration, error)

Calculate the age of the resource

func (*Resource) DateAfter

func (r *Resource) DateAfter(d time.Time) bool

func (*Resource) Expires

func (r *Resource) Expires() (time.Time, error)

func (*Resource) HasExplicitExpiration

func (r *Resource) HasExplicitExpiration() bool

func (*Resource) HasValidators

func (r *Resource) HasValidators() bool

func (*Resource) Header

func (r *Resource) Header() http.Header

func (*Resource) HeuristicFreshness

func (r *Resource) HeuristicFreshness() time.Duration

func (*Resource) IsNonErrorStatus

func (r *Resource) IsNonErrorStatus() bool

func (*Resource) IsStale

func (r *Resource) IsStale() bool

func (*Resource) LastModified

func (r *Resource) LastModified() time.Time

func (*Resource) MarkStale

func (r *Resource) MarkStale()

func (*Resource) MaxAge

func (r *Resource) MaxAge(shared bool) (time.Duration, error)

func (*Resource) MustValidate

func (r *Resource) MustValidate(shared bool) bool

func (*Resource) RemovePrivateHeaders

func (r *Resource) RemovePrivateHeaders()

func (*Resource) Status

func (r *Resource) Status() int

func (*Resource) Via

func (r *Resource) Via() string

type ResponseStreamer

type ResponseStreamer struct {
	StatusCode int
	http.ResponseWriter
	*stream.Stream
	// C will be closed by WriteHeader to signal the headers' writing
	C chan struct{}
}

func NewResponseStreamer

func NewResponseStreamer(w http.ResponseWriter) *ResponseStreamer

func (*ResponseStreamer) Close

func (rs *ResponseStreamer) Close() error

func (*ResponseStreamer) Resource

func (rs *ResponseStreamer) Resource() *Resource

Resource returns a copy of the responseStreamer as a Resource object

func (*ResponseStreamer) WaitHeaders

func (rs *ResponseStreamer) WaitHeaders()

WaitHeaders returns iff and when WriteHeader has been called.

func (*ResponseStreamer) Write

func (rs *ResponseStreamer) Write(b []byte) (int, error)

func (*ResponseStreamer) WriteHeader

func (rs *ResponseStreamer) WriteHeader(status int)

type Validator

type Validator struct {
	Handler http.Handler
}

func (*Validator) Validate

func (v *Validator) Validate(req *http.Request, res *Resource) bool

Jump to

Keyboard shortcuts

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