templates.plugins.spincast-http-caching.spincast-http-caching.html Maven / Gradle / Ivy
Show all versions of spincast-website Show documentation
{#==========================================
Spincast HTTP Caching plugin
==========================================#}
{% extends "../../layout.html" %}
{% block sectionClasses %}plugins plugins-spincast-http-caching{% endblock %}
{% block meta_title %}Plugins - Spincast HTTP Caching{% endblock %}
{% block meta_description %}Spincast HTTP Caching plugin : ETag, Cache-Control, etc.{% endblock %}
{% block scripts %}
{% endblock %}
{% block body %}
Overview
The Spincast HTTP Caching plugin provides utilities to
send and validate cache headers. The main component is the
request context add-on, ICacheHeadersRequestContextAddon.
For the full documentation about HTTP caching using Spincast, please have a look at the
HTTP caching section.
Installation
If you use the spincast-default artifact, this plugin is already installed so
you have nothing more to do!
If you start from scratch using the spincast-core artifact, you can use the
plugin by adding this artifact to your project:
<dependency>
<groupId>org.spincast</groupId>
<artifactId>spincast-plugins-http-caching</artifactId>
<version>{{spincastCurrrentVersion}}</version>
</dependency>
You then install the plugin's Guice module, by passing it to the Guice.createInjector(...) method:
Injector guice = Guice.createInjector(
new SpincastCoreGuiceModule(args),
new SpincastHttpCachingPluginGuiceModule(IAppRequestContext.class,
IAppWebsocketContext.class)
// other modules...
);
... or by using the install(...) method from your custom Guice module:
public class AppModule extends SpincastCoreGuiceModule {
@Override
protected void configure() {
super.configure();
install(new SpincastHttpCachingPluginGuiceModule(getRequestContextType(),
getWebsocketContextType()));
// other modules...
}
// ...
}
The request context add-on
Quick example :
public void myHandler(IAppRequestContext context) {
// Sends "Cache-Control" header
context.cacheHeaders().cache(60);
// Validates the "freshness" headers
if(context.cacheHeaders().eTag(etag).lastModified(date).validate(true)) {
return;
}
}
Methods :
-
ICacheHeadersRequestContextAddon<R> eTag(String currentTag)
Specifies the current ETag (strong) of the resource.
A strong comparison will be used to compare the request ETag to this
current ETag.
Skip, or use null if the resource doesn't exist.
-
ICacheHeadersRequestContextAddon<R> eTag(String currentTag, boolean currentTagIsWeak)
Specifies the current strong or weak ETag of the resource.
A strong comparison will be used to compare the request ETag to this
current ETag.
Skip, or use null if the resource doesn't exist.
-
ICacheHeadersRequestContextAddon<R> eTag(String currentTag, boolean currentTagIsWeak, boolean weakComparison)
Specifies the current strong or weak ETag of the resource.
Skip, or use null if the resource doesn't exist.
-
ICacheHeadersRequestContextAddon<R> lastModified(Date lastModificationDate)
Specifies the last modification date of the resource.
Skip, or use null if the resource doesn't exist.
-
ICacheHeadersRequestContextAddon<R> cache(int seconds)
The number of seconds the client should cache this resource
before requesting it again.
-
ICacheHeadersRequestContextAddon<R> cache(int seconds, boolean isPrivate)
The number of seconds the client should cache this resource
before requesting it again.
-
ICacheHeadersRequestContextAddon<R> cache(int seconds, boolean isPrivate, Integer secondsCdn)
The number of seconds the client should cache this resource
before requesting it again.
-
ICacheHeadersRequestContextAddon<R> noCache()
Sends "No Cache" headers so the resource is not cached
at all by the client.
-
boolean validate(boolean resourceCurrentlyExists)
Call this when you are done setting
ETag and/or Last-Modified to validate
them agains the headers sent by the client.
If this method returns true, the route handler should return immediately
without returning/creating/modifying/deleting the associated resource! Appropriate headers
have already been set on the response.
{% endblock %} 

Spincast HTTP Caching