This is an archive of an inactive wiki and cannot be modified.

Caching with Entity Tags

This technique gives a possible implementation for the CategoryBpCaching. It can be achieved by using several different technologies.

Dynamic mechanisms can be used to serve documents only if their content and/or modify date has changed by using HTTP headers ETag, If-None-Match and Last-Modifed. ETag (short form for Entity Tag) exists to express the representation of a dynamic web resource which depends on several factors (time or different logical conditions, for example). For instance, an ETag associated to a given resource might be a checksum of its content (calculated via MD5 algorithm, for example).

This way, a user agent requests a server for a document:

UA --> HTTP Server

GET /news/latest.html HTTP/1.1
Host: example.com

The server then generates a response, containing an ETag header with an associated value calculated as the checksum of the content of the document:

UA <-- HTTP Server

HTTP/1.1 200 OK
Server: MyServer/2.1
Date: Thu, 09 Feb 2006 13:30:54 GMT
Content-Type: text/html
Accept-Ranges: bytes
Last-Modified: Tue, 09 Feb 2006 12:00:00 GMT
ETag: "6d82cbb050ddc7fa9cbb659014546e59"
Content-Length: 363

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
...
</html>

User agent then extracts the value of the ETag (6d82cbb050ddc7fa9cbb659014546e59, in the example) and next request will include a If-None-Match header with that checksum:

UA --> HTTP Server

GET /news/latest.html HTTP/1.1
If-None-Match: "6d82cbb050ddc7fa9cbb659014546e59"
Host: example.com

If the document has not changed, it will have the same associated ETag so it will match the value of If-None-Match header in the new request, thus sending an HTTP 304 response:

UA <-- HTTP Server

HTTP/1.1 304 Not Modified
Server: MyServer/2.1
Date: Fri, 10 Feb 2006 09:00:00 GMT
ETag: "6d82cbb050ddc7fa9cbb659014546e59"
Content-Length: 0

This way, user agent would realize that the local version of the document (previously stored) is the freshest one and then display it to the user.

If the document had changed, the checksum calculated by the server would be different to the value in request's If-None-Match header and then the server would generate an HTTP 200 response contaning the document itself:

HTTP/1.1 200 OK
Server: MyServer/2.1
Date: Thu, 09 Feb 2006 13:30:54 GMT
Content-Type: text/html
Accept-Ranges: bytes
Last-Modified: Tue, 09 Feb 2006 12:00:00 GMT
ETag: " c98cde386b68208e3562db6a4ecabef4"
Content-Length: 330

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
...
</html>

Latter requests from the UA will include If-None-Match with the checksum of the latest accessed version of the document.

Another version of caching mechanism might be implemented by using the date of the last changed applied to a document or resource in a server. The interaction between user agent and server would be similar to ETag/If-None-Match dialog, but using Last-Modified HTTP response header and If-Modified-Since HTTP request header. These headers have date values in the format specified by RFC-850 (adding suffix GMT).

Some URIs of interest:

Back to BestPracticesList



CategoryBpCaching

Contributions to this wiki are governed by the W3C policies for Contribution to W3C' wiki on Mobile Web.