etc_cache

2017-03-18 13:46

Keywords: smart, fast, cache

Textpattern is fast, but when you have thousands of articles, processing the whole list (say, for creating a sitemap) can become time consuming. It is then a good idea (unless you publish an article every minute) to cache the processed result. Naturally, the cached block must be updated when some article is added/deleted. Most caching plugins trigger this update when the corresponding page is visited after the site update. This has, however, two inconveniences:

  • the first visitor has to wait while the expired block is processed and cached again;
  • every site modification, even irrelevant to the cached content, yields the cache update.

That’s what etc_cache is aimed to solve. The basic usage is

<txp:etc_cache id="heavycode">
	<!-- heavy code -->
</txp:etc_cache>

The code will be processed and cached until the site is updated. On site update, the plugin (if configured so) will ping the url containing this block, triggering the cache refresh. Hence, the cache will always stay up to date, without penalizing site visitors.

To configure automatic cache updates, visit Extensions/Cache tab and edit reset field of each block. The possible values are:

  • (empty): update client-side only when expired, regardless the site updates;
  • 1 (default): update client-side if expired or the site was updated;
  • a list of events like article_posted,article_saved or SQL LIKE pattern like article%: update server-side when a matching event is fired.
  • The value % thus means “auto-update on each site update”, but will act as 1 client-side too.

You can be more specific with cache reset criteria. Say, if you need a block to be reset only if the article 3 is updated, set

reset: article_saved
filter: {"article_saved":{"ID":3}}

You can also pass reset attribute directly to etc_cache:

<txp:etc_cache id="archive" reset="article%">
	<!-- heavy code building an articles archive -->
</txp:etc_cache>

If needed, one can pass time attribute to etc_cache:

<txp:etc_cache id="dailycode" time="+1 day">
	<!-- daily code -->
</txp:etc_cache>

A positive (relative) value of time will indicate that the cache (even a fresh one) must be reset on site update. A negative value will not observe site updates. An absolute value like time='<txp:modified format="%F %T" gmt="1" /> +1 month' will mean “cache it if not modified since one month”.

The experimental zipped version is for txp 4.8+ only. It enables the full-page disk cache and direct access, avoiding all php/txp processing. Requires a bit of .htaccess configuration, don’t hesitate to ask for assistance.

File(s)