ETags are used in to check whether pages need to be re-calculated or can be
served from cache. An ETag is simply a string. Under
it is a string of tokens separated by pipe characters. The tokens hold values
such as a user id, the current skin name, or a counter indicating how many
objects have been added to the site. The idea is that the browser sends a
request with the ETag included in an
If-None-Match header. Plone can then
quickly calculate the current ETag for the requested resource. If the ETag
is the same, Plone can reply with
304 NOT MODIFIED response, telling the
browser to use its cached copy. Otherwise, Plone renders the page and returns
it as normal.
Many caching operations use ETags. The tokens to include are typically
listed in an
etags tuple in the operation's options.
The ETag names tokens supported by default are:
The current user's id
A list of the current user's roles in the given context
The language(s) accepted by the browser, in the
The current user's preferred language
A timestamp indicating the last-modified date of the given context
A counter that is incremented each time the catalog is updated, i.e. each time content in the site is changed.
Whether or not the given context is locked for editing.
The name of the current skin (theme)
A timestamp indicating the most recent last-modified date for all three Resource Registries. This is useful for avoiding requests for expired resources from cached pages.
It is possible to provide additional tokens by registering an
adapter. This should be a named adapter on the published object (typically a
view, file resource or Zope page template object) and request, with a unique
name. The name is used to look up the component. Thus, you can also override
one of the tokens above for a particular type of context or request (e.g. via
a browser layer), by registering a more specific adapter with the same name.
As an example, here is the
from zope.interface import implements from zope.interface import Interface from zope.component import adapts from plone.app.caching.interfaces import IETagValue class Language(object): """The ``language`` etag component, returning the value of the HTTP_ACCEPT_LANGUAGE request key. """ implements(IETagValue) adapts(Interface, Interface) def __init__(self, published, request): self.published = published self.request = request def __call__(self): return self.request.get('HTTP_ACCEPT_LANGUAGE', '')
This is registered in ZCML like so:
<adapter factory=".etags.Language" name="language" />