cache

package
v11.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2018 License: BSD-3-Clause, BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PragmaHeaderKey is the header key of "Pragma".
	PragmaHeaderKey = "Pragma"
	// PragmaNoCacheHeaderValue is the header value of "Pragma": "no-cache".
	PragmaNoCacheHeaderValue = "no-cache"
	// ExpiresHeaderKey is the header key of "Expires".
	ExpiresHeaderKey = "Expires"
	// ExpiresNeverHeaderValue is the header value of "ExpiresHeaderKey": "0".
	ExpiresNeverHeaderValue = "0"
)

Variables

View Source
var Cache304 = func(expiresEvery time.Duration) context.Handler {
	return func(ctx context.Context) {
		now := time.Now()
		if modified, err := ctx.CheckIfModifiedSince(now.Add(-expiresEvery)); !modified && err == nil {
			ctx.WriteNotModified()
			return
		}

		ctx.SetLastModified(now)
		ctx.Next()
	}
}

Cache304 sends a `StatusNotModified` (304) whenever the "If-Modified-Since" request header (time) is before the time.Now() + expiresEvery (always compared to their UTC values). Use this `cache#Cache304` instead of the "github.com/kataras/iris/cache" or iris.Cache for better performance. Clients that are compatible with the http RCF (all browsers are and tools like postman) will handle the caching. The only disadvantage of using that instead of server-side caching is that this method will send a 304 status code instead of 200, So, if you use it side by side with other micro services you have to check for that status code as well for a valid response.

Developers are free to extend this method's behavior by watching system directories changes manually and use of the `ctx.WriteWithExpiration` with a "modtime" based on the file modified date, can be used on Party's that contains a static handler, i.e `StaticWeb`, `StaticEmbedded` or even `StaticEmbeddedGzip`.

View Source
var CacheControlHeaderValue = "private, no-cache, max-age=0, must-revalidate, no-store, proxy-revalidate, s-maxage=0"

CacheControlHeaderValue is the header value of the "Cache-Control": "private, no-cache, max-age=0, must-revalidate, no-store, proxy-revalidate, s-maxage=0".

It can be overridden.

View Source
var ETag = func(ctx context.Context) {
	key := ctx.Request().URL.Path
	ctx.Header(context.ETagHeaderKey, key)
	if match := ctx.GetHeader(ifNoneMatchHeaderKey); match == key {
		ctx.WriteNotModified()
		return
	}
	ctx.Next()
}

ETag is another browser & server cache request-response feature. It can be used side by side with the `StaticCache`, usually `StaticCache` middleware should go first. This should be used on routes that serves static files only. The key of the `ETag` is the `ctx.Request().URL.Path`, invalidation of the not modified cache method can be made by other request handler as well.

In typical usage, when a URL is retrieved, the web server will return the resource's current representation along with its corresponding ETag value, which is placed in an HTTP response header "ETag" field:

ETag: "/mypath"

The client may then decide to cache the representation, along with its ETag. Later, if the client wants to retrieve the same URL resource again, it will first determine whether the local cached version of the URL has expired (through the Cache-Control (`StaticCache` method) and the Expire headers). If the URL has not expired, it will retrieve the local cached resource. If it determined that the URL has expired (is stale), then the client will contact the server and send its previously saved copy of the ETag along with the request in a "If-None-Match" field.

Usage with combination of `StaticCache`: assets := app.Party("/assets", cache.StaticCache(24 * time.Hour), ETag) assets.StaticWeb("/", "./assets") or StaticEmbedded("/", "./assets") or StaticEmbeddedGzip("/", "./assets").

Similar to `Cache304` but it doesn't depends on any "modified date", it uses just the ETag and If-None-Match headers.

Read more at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching and https://en.wikipedia.org/wiki/HTTP_ETag

NoCache is a middleware which overrides the Cache-Control, Pragma and Expires headers in order to disable the cache during the browser's back and forward feature.

A good use of this middleware is on HTML routes; to refresh the page even on "back" and "forward" browser's arrow buttons.

See `cache#StaticCache` for the opposite behavior.

View Source
var StaticCache = func(cacheDur time.Duration) context.Handler {
	if int64(cacheDur) <= 0 {
		return NoCache
	}

	cacheControlHeaderValue := "public, max-age=" + strconv.Itoa(int(cacheDur.Seconds()))
	return func(ctx context.Context) {
		cacheUntil := time.Now().Add(cacheDur).Format(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
		ctx.Header(ExpiresHeaderKey, cacheUntil)
		ctx.Header(context.CacheControlHeaderKey, cacheControlHeaderValue)

		ctx.Next()
	}
}

StaticCache middleware for caching static files by sending the "Cache-Control" and "Expires" headers to the client. It accepts a single input parameter, the "cacheDur", a time.Duration that it's used to calculate the expiration.

If "cacheDur" <=0 then it returns the `NoCache` middleware instaed to disable the caching between browser's "back" and "forward" actions.

Usage: `app.Use(cache.StaticCache(24 * time.Hour))` or `app.Use(cache.Staticcache(-1))`. A middleware, which is a simple Handler can be called inside another handler as well, example: cacheMiddleware := cache.StaticCache(...)

func(ctx iris.Context){
 cacheMiddleware(ctx)
 [...]
}

Functions

func Cache

func Cache(expiration time.Duration) *client.Handler

Cache accepts the cache expiration duration if the expiration <=2 seconds then expiration is taken by the "cache-control's maxage" header returns context.Handler, which you can use as your default router or per-route handler

All types of response can be cached, templates, json, text, anything.

Use it for server-side caching, see the `iris#Cache304` for an alternative approach that may fit your needs most.

You can add validators with this function.

func Handler

func Handler(expiration time.Duration) context.Handler

Handler accepts one single parameter: the cache expiration duration if the expiration <=2 seconds then expiration is taken by the "cache-control's maxage" header returns context.Handler.

All types of response can be cached, templates, json, text, anything.

Use it for server-side caching, see the `iris#Cache304` for an alternative approach that may fit your needs most.

it returns a context.Handler which can be used as a middleware, for more options use the `Cache`.

Examples can be found at: https://github.com/kataras/iris/tree/master/_examples/#caching

Types

This section is empty.

Directories

Path Synopsis
Package ruleset provides the basics rules which are being extended by rules.
Package ruleset provides the basics rules which are being extended by rules.

Jump to

Keyboard shortcuts

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