httpcache

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2014 License: MIT Imports: 20 Imported by: 0

README

httpcache

httpcache provides an rfc7234 compliant golang http.Handler.

wercker status

GoDoc

Example

This example if from the included CLI, it runs a caching proxy on http://localhost:8080.

proxy := &httputil.ReverseProxy{
    Director: func(r *http.Request) {
    },
}

handler := httpcache.NewHandler(httpcache.NewMemoryCache(), proxy)
handler.Shared = true

log.Printf("proxy listening on http://%s", listen)
log.Fatal(http.ListenAndServe(listen, proxy))

Implemented

  • All of rfc7234, except those listed below
  • Disk and Memory storage
  • Apache-like logging via httplog package

Todo

  • Caching of conditional requests
  • Handling Authorization header correctly (currently uncacheable)
  • Handle Cache-Control directives that can be limited to certain headers
  • Offline operation
  • LRU metadata, cache eviction
  • Size constraints on memory/disk cache
  • Edge cases around handling chunked upstream responses and HTTP/1.0 clients
  • Proper handling of Via header
  • Respect max-age headers in Requests
  • Support for weak entities with If-Match and If-None-Match
  • Invalidation based on Content-Location

Testing

Tests are currently conducted via the test suite and verified via the CoAdvisor tool.

Reading List

Documentation

Index

Constants

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

Variables

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

Returned when a resource doesn't exist

Functions

func Debugf

func Debugf(format string, args ...interface{})

func Errorf

func Errorf(format string, args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(format string, args ...interface{})

Types

type Cache

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

Cache provides a storage mechanism for cached Resources

func NewCache

func NewCache(fs vfs.VFS) *Cache

NewCache returns a Cache backed off the provided VFS

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 (*Cache) Freshen

func (c *Cache) Freshen(res *Resource, keys ...string) error

func (*Cache) Header

func (c *Cache) Header(key string) (Header, error)

Retrieve the Status and Headers for a given key path

func (*Cache) Invalidate

func (c *Cache) Invalidate(keys ...string)

func (*Cache) Retrieve

func (c *Cache) Retrieve(key string) (*Resource, error)

Retrieve returns a cached Resource for the given key

func (*Cache) Store

func (c *Cache) Store(res *Resource, keys ...string) error

Store a resource against a number of keys

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 Handler

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

func NewHandler

func NewHandler(cache *Cache, upstream http.Handler) *Handler

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request)
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

RequestKey generates a Key for a request

func (Key) ForMethod

func (k Key) ForMethod(method string) Key

ForKey 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 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) 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) Status

func (r *Resource) Status() int

func (*Resource) Via

func (r *Resource) Via() string

func (*Resource) Warnings

func (r *Resource) Warnings() ([]string, error)

type Validator

type Validator struct {
	Handler http.Handler
}

func (*Validator) Validate

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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