📜 ⬆️ ⬇️

Proper REST caching

Suppose we want to write our habrakhabr with blackjack and other delights. The page of the article we have consists of 3 voluminous blocks:
1. the actual text of the article. changes very rarely.
2. comment tree. changes relatively often, but with time less and less.
3. live broadcast. small, but changes very often.

Suppose that the page with this article is available at? Article: right.cache
But inside it we will not put any content, and we will carry it into separate resources, as is usually done with scripts and styles. Inside? Article: right.cache will only be an index of connected resources with versions.

? article: right.cache / content / version: 123
? article: right.cache / comments / time: 2010-12-01
? live / time: 2010-12-01
? style: article / version: 666
? script: article / version: 333
')
Specifying a version allows you to specify hard caching for resources. And for the index file, on the contrary, let's set the need to check with each request whether it has changed.

Such an organization guarantees us that when new comments appear, we will not have to load the article again. Conversely, when you change the article will not have to overload the entire tree of comments. And about the fact that because of the often changing live broadcast, we don’t need to load all the content on a new one, and we should not stutter ;-)

It is important that search engines see links to resources and index them. However, people will come from a search to a specific resource and even to a specific version of it. Accordingly, the resource must determine whether it is loaded by a direct link, and if so, after downloading by client tools, redirect to the index. If the current version of the resource has not changed, it will then be taken from the cache. If it has changed, a new version will be downloaded. Not such a terrible trouble, in fact ;-)

Implementations using frames and ajax are pretty commonplace, so let's use html includas .

The index is nothing new.

<! DOCTYPE t:stylesheet [ <! ATTLIST t:stylesheet id ID # REQUIRED > ] > <br> <? xml-stylesheet type ="text/xsl" href ="#t:stylesheet" ? > <br> < t:stylesheet id ="t:stylesheet" version ="1.0" xmlns:t ="http://www.w3.org/1999/XSL/Transform" > <br> < t:output doctype-public ="-//W3C//DTD XHTML 2.0//EN" doctype-system ="http://www.w3.org/MarkUp/DTD/xhtml2.dtd" /> <br> < t:template match =" @* | node() " > <br> < t:copy > <br> < t:apply-templates select =" @* | node() " /> <br> </ t:copy > <br> </ t:template > <br> < t:template match =" *[ @src and contains( @srctype, 'xml' ) ] " > <br> < t:copy > <br> < t:apply-templates select =" @* " /> <br> < t:apply-templates select =' document( @src )//html/body/* ' /> <br> </ t:copy > <br> </ t:template > <br> < t:template match =" / " > <br> < t:apply-templates select =" document( '#t:stylesheet' )//html " /> <br> </ t:template > <br> < t:template name ="content" > <br> < html > <br> < head > <br> < title > Article with comments </ title > <br> < meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" /> <br> < link href ="?style:article/version:666" type ="text/css" rel ="stylesheet" /> <br> < script src ="?script:article/version:333" type ="text/javascript" ></ script > <br> </ head > <br> < body > <br> < section src ="?article:right.cache/content/version:123" srctype ="text/xml" > <br> < a href ="?article:right.cache/content/version:123" > article content </ a > <br> </ section > <br> < section src ="?article:right.cache/comments/time:2010-12-01" srctype ="text/xml" > <br> < a href ="?article:right.cache/comments/time:2010-12-01" > article comments </ a > <br> </ section > <br> < section src ="?live/time:2010-12-01" srctype ="text/xml" > <br> < a href ="?live/time:2010-12-01" > live content </ a > <br> </ section > <br> </ body > <br> </ html > <br> </ t:template > <br> </ t:stylesheet >


But the resource will have other templates.

<! DOCTYPE t:stylesheet [ <! ATTLIST t:stylesheet id ID # REQUIRED > ] > <br> <? xml-stylesheet type ="text/xsl" href ="#t:stylesheet" ? > <br> < t:stylesheet id ="t:stylesheet" version ="1.0" xmlns:t ="http://www.w3.org/1999/XSL/Transform" > <br> < t:output doctype-public ="-//W3C//DTD XHTML 2.0//EN" doctype-system ="http://www.w3.org/MarkUp/DTD/xhtml2.dtd" /> <br> < t:template name ="content" > <br> < html > <br> < head > <br> < title > Article content </ title > <br> < meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" /> <br> </ head > <br> < body > <br> < h > Article content </ h > <br> </ body > <br> </ html > <br> </ t:template > <br> < t:template match =" / " > <br> < t:element name ="meta" > <br> < t:attribute name ="http-equiv" > refresh </ attribute > <br> < t:attribute name ="content" > 0;url=?article:right.cache </ attribute > <br> </ t:element > <br> </ t:template > <br> </ t:stylesheet >

With a direct browser request, it will redirect to? Article: right.cache
The search engine will get the content and index it.

Source: https://habr.com/ru/post/90481/


All Articles