httpcache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2016 License: MIT Imports: 10 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// GC checks for expired cache entries and deletes them every tick of this time
	// set it to <=2*time.Second to disable GC for all default cache services'  memory stores
	GC = 30 * time.Minute

	// Fasthttp is the package-level instance of valyala/fasthttp cache service with a memory store
	// has the same functions as httpcache and httpcache.NewService(), which is the net/http cache
	//
	// USAGE:
	// fasthttp.ListenAndServe("mydomain.com",httpcache.Fasthttp.Cache(myRouter, 20* time.Second))
	// or for individual handlers:
	// httpcache.Fasthttp.Cache(myRequestHandler, 20* time.Secnd)
	//
	Fasthttp = NewServiceFasthttp(defaultStore)

	// Default is default package-level instance of the net/http cache service with a memory store
	//
	// USAGE:
	// http.ListenAndServe("mydomain.com",httpcache.Cache(myRouter, 20* time.Second))
	// or for individual handlers:
	// httpcache.Cache(myHandler, 20* time.Secnd)
	//
	Default = NewService(defaultStore)
)
View Source
var Client = &http.Client{Timeout: requestCacheTimeout}

Client is used inside the global Request function this client is an exported to give you a freedom of change its Transport, Timeout and so on(in case of ssl)

View Source
var ClientFasthttp = &fasthttp.Client{WriteTimeout: requestCacheTimeout, ReadTimeout: requestCacheTimeout}

ClientFasthttp is used inside the global RequestFasthttp function this client is an exported variable because the maybe the remote cache service is running behind ssl, in that case you are able to set a Transport inside it

Functions

func Cache

func Cache(bodyHandler http.Handler, expiration time.Duration) http.Handler

Cache accepts two parameters first is the http.Handler which you want to cache its result the second is, optional, the cache Entry's expiration duration if the expiration <=2 seconds then expiration is taken by the "cache-control's maxage" header returns an http.Handler, which you can use as your default router or per-route handler

All type of responses are cached, templates, json, text, anything.

If you use only one global cache for all of your routes use the `httpcache.New` instead

func Clear

func Clear()

Clear removes all entries from the default cache

func Invalidate

func Invalidate(req *http.Request)

Invalidate accepts a *http.Request which is used to find the cache key and removes any releated Entry from the cache

func Request

func Request(remoteURL string, bodyHandler http.Handler, expiration time.Duration) http.Handler

Request , or remote cache client whatever you like, it's the client-side function of the ServeHTTP sends a request to the server-side remote cache Service and sends the cached response to the frontend client it is used only when you achieved something like horizontal scaling (separate machines) look ServeHTTP for more

if cache din't find then it sends a POST request and save the bodyHandler's body to the remote cache.

It takes 3 parameters the first is the remote address (it's the address you started your http server which handled by the Service.ServeHTTP) the second is the handler (or the mux) you want to cache and the third is the, optionally, cache expiration, which is used to set cache duration of this specific cache entry to the remote cache service if <=minimumAllowedCacheDuration then the server will try to parse from "cache-control" header

client-side function

func RequestFasthttp

func RequestFasthttp(remoteURL string, bodyHandler fasthttp.RequestHandler, expiration time.Duration) fasthttp.RequestHandler

RequestFasthttp , it's the client-side function, sends a request to the server-side remote cache Service and parses the cached response it is used only when you achieved something like horizontal scaling (separate machines) and you have an already running remote cache Service or ServiceFasthttp look ServeHTTP for more

if cache din't find then it sends a POST request and save the bodyHandler's body to the remote cache.

It takes 3 parameters the first is the remote address (it's the address you started your http server which handled by the Service.ServeHTTP) the second is the handler (or the mux) you want to cache and the third is the, optionally recommending, cache expiration which is used to set cache duration of this specific cache entry to the remote cache service

client-side function

func ServeHTTP

func ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP serves the cache Service to the outside world, it is used only when you want to achieve something like horizontal scaling (separate machines) it parses the request and tries to return the response with the cached body of the requested cache key server-side function

Types

type Entry

type Entry struct {
	StatusCode  int
	ContentType string
	Body        []byte
	// we could have a new Timer foreach cache Entry in order to be persise on the expiration but this will cost us a lot of performance,
	// (the ticker should be stopped if delete or key ovveride and so on...)
	// but I chosen to just have a generic timer with its tick on the lowest 'expires' of all cache entries that cache keeps
	Expires time.Time
}

Entry is the entry type of the cached items contains the http status code, content type the full cached response body(any type of it) and the expiration datetime

type Service

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

Service contains the cache and performs actions to save cache and serve cached content

func NewService

func NewService(store Store) *Service

NewService returns a new cache Service with the given store

func (*Service) Cache

func (s *Service) Cache(bodyHandler http.Handler, expiration time.Duration) http.Handler

Cache accepts two parameters first is the http.Handler which you want to cache its result the second is, optional, the cache Entry's expiration duration if the expiration <=2 seconds then expiration is taken by the "cache-control's maxage" header returns an http.Handler, which you can use as your default router or per-route handler

All type of responses are cached, templates, json, text, anything.

If you use only one global cache for all of your routes use the `httpcache.New` instead

func (*Service) Clear

func (s *Service) Clear()

Clear removes all cache entries(the GC still running)

func (*Service) Invalidate

func (s *Service) Invalidate(req *http.Request)

Invalidate accepts a *http.Request which is used to find the cache key and removes any releated entry from the cache

func (*Service) ServeHTTP

func (s *Service) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP serves the cache Service to the outside world, it is used only when you want to achieve something like horizontal scaling it parses the request and tries to return the response with the cached body of the requested cache key server-side function

type ServiceFasthttp

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

ServiceFasthttp contains the cache and performs actions to save cache and serve cached content

func NewServiceFasthttp

func NewServiceFasthttp(store Store) *ServiceFasthttp

NewServiceFasthttp returns a new cache Service with the given store

func (*ServiceFasthttp) Cache

func (s *ServiceFasthttp) Cache(bodyHandler fasthttp.RequestHandler, expiration time.Duration) fasthttp.RequestHandler

Cache accepts two parameters first is the http.Handler which you want to cache its result the second is, optional, the cache Entry's expiration duration if the expiration <=2 seconds then expiration is taken by the "cache-control's maxage" header returns an http.Handler, which you can use as your default router or per-route handler

All type of responses are cached, templates, json, text, anything.

If you use only one global cache for all of your routes use the `httpcache.New` instead

func (*ServiceFasthttp) Clear

func (s *ServiceFasthttp) Clear()

Clear clears all the cache (the GC still running)

func (*ServiceFasthttp) Invalidate

func (s *ServiceFasthttp) Invalidate(reqCtx *fasthttp.RequestCtx)

Invalidate accepts a *http.Request which is used to find the cache key and removes any releated Entry from the cache

func (*ServiceFasthttp) ServeHTTP

func (s *ServiceFasthttp) ServeHTTP(reqCtx *fasthttp.RequestCtx)

ServeHTTP serves the cache ServiceFasthttp to the outside world, it is used only when you want to achieve something like horizontal scaling it parses the request and tries to return the response with the cached body of the requested cache key server-side function

type Store

type Store interface {
	// Set adds an entry to the cache by its key
	// entry must contain a valid status code, conten type, a body and optional, the expiration duration
	Set(key string, statusCode int, cType string, body []byte, expiration time.Duration)
	// Get returns an entry based on its key
	Get(key string) *Entry
	// Remove removes an entry from the cache
	Remove(key string)
	// Clear removes all entries from the cache
	Clear()
}

Store is the interface of the cache bug, default is memory store for performance reasons

func NewMemoryStore

func NewMemoryStore(gcDuration time.Duration) Store

NewMemoryStore returns a new memory store for the cache , note that httpcache package provides one global default cache service which provides these functions: `httpcache.Cache`, `httpcache.Invalidate` and `httpcache.Start`

If you use only one global cache for all of your routes use the `httpcache.New` instead

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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