"Perfect cache" in Enonic CMS 4.4
The client-side caching has been revamped in Enonic CMS 4.4. In previous versions, a client was instructed to cache an element for a specific period only, and changes to the element was detected after this timer had expired. Now, bandwith-consuming data like images and files are cached locally "forever", but changes to the data will be detected immediately.
Caching on the client or proxy will effectively reduces the number of requests sent to the server. Instead, needed resources and data will be stored on the client itself, boosting performance significantly (in particular in environments with limited bandwidth).
An ideal client-side cache will ensure that a bandwidth-consuming element is downloaded to the client only once, caching the element until a change to that element occurs. If so, the new version should immediately be downloaded and cached it until further changes occurs.
This "perfect cache" approach is implemented for images, static resources and attachments in Enonic CMS 4.4. To accheive this, HTTP Cache Headers for images, resources and attachments must be enabled, and the URLs to these elements must be generated using the built-in XSLT functions "createAttachmentUrl", "createResourceUrl" and "createImageUrl". This will make sure that the client will receive cache-instructions, and a timestamp will be attached as a parameter to the rendered URL.
Enabling HTTP Cache Headers
By default, this is enabled, so its probably not necessary to do any changes, but HTTP Cache Headers will be enabled if these options are set to true in your site-X.properties files:
cms.site.page.http.cacheHeadersEnabled = true cms.site.attachment.http.cacheHeadersEnabled = true cms.site.resource.http.cacheHeadersEnabled = true cms.site.image.http.cacheHeadersEnabled = true
See the online documentation for detailed information on caching options.
An example
As an example, when including an image on your site:
portal:createImageUrl("190", "invert", "0x000000", "png")
The result of the portal function is an URL to the image::
http://www.enonic.com/_image/87033.png?_encoded=2f6666..73&_ts=124541c204a
The _ts - parameter is a timestamp indicating when the image was changed. When the image is fetched through the portal, the response header will roughly look like this:
| Date |
Fri, 18 Dec 2009 09:45:42 GMT
|
| Server |
Apache/2.2.3 (CentOS)
|
| Expires |
Sat, 18 Dec 2010 09:46:08 GMT
|
| Cache-Control |
private, max-age=31536000
|
| Content-Disposition |
inline;filename="binary-87033-42761"
|
| Content-Length |
46144
|
| Connection |
close
|
| Content-Type |
image/png;charset=UTF-8
|
The "expires" and "Cache-control" headers will instruct the client or proxy to cache this image for one year.
If a change is done to the image, the next time the function to createImageUrl for attachmentkey 190 i called, the rendered URL will look like this:
http://www.enonic.com/_image/87033.png?_encoded=2f6666..73&_ts124542b314b
The result of this request is not cached on the client, so it will fetch the image from the server again.




Comments
If you want to comment on this article you need to be logged in.