Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
etc_cache: cache trying hard to be smart
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 will ping the url containing this block, triggering the cache refresh. Hence, the cache will always stay up to date, without penalizing site visitors.
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, edit (Extensions tab) the following fields of etc_cache
table:
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_posted,article_saved,articles_deleted,articles_updated">
<!-- 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”.
Info and download. Note that etc_cache
requires txp 4.6+ to work properly.
Offline
Re: etc_cache: cache trying hard to be smart
Hi Oleg,
Does this do a similar function to aks_cache? i.e. can it cache a form for a period of time (say, 5 minutes) regardless of whether the site itself is updated.
Usage case: I have a form that fetches some data from an external site – I want that data to be cached in my form for a few minutes rather than have every visitor request that external data.
Offline
Re: etc_cache: cache trying hard to be smart
Hi Phil,
yes, a negative value should do it:
<txp:etc_cache id="external" time="-300">
...
</txp:etc_cache>
Offline
Re: etc_cache: cache trying hard to be smart
Great, thanks. Will test this out (seems to work in my limited testing).
Offline
Re: etc_cache: cache trying hard to be smart
Hi Oleg, any chance you can add this to the etc-plugins repo collection so I can add Composer support and make a couple of tweaks to table layouts?
Also, the negative time value (such as time="-300"
for 5 minutes in seconds) – can that accept things such as -1 day
and -1 week
and so on?
Offline
Re: etc_cache: cache trying hard to be smart
Hi Phil, I have created an empty repo, will import files later (or please feel free to add what you dim necessary).
Negative literal values should work as soon as strtotime()
interprets them correctly, -1 week
etc should be ok.
Offline
Re: etc_cache: cache trying hard to be smart
Hi Oleg,
I can’t commit to the etc_cache repo, have you added me as a collaborator for that one? I’ve got some updates ready to put into it when I get access.
Offline
Re: etc_cache: cache trying hard to be smart
Uh.. sorry Phil, I thought you had admin access to the whole etc-plugins repo. You should be owner now, sorry again.
Offline
Re: etc_cache: cache trying hard to be smart
Latest release of this plugin is now available on GitHub (or via Composer). I’ve updated the admin panel layout a bit plus better documentation.
Offline
Re: etc_cache: cache trying hard to be smart
Is there a way of disabling caching globally temporarily, for example via a prefs switch or in debugging* mode?
I’m currently doing flat theme updates to a site and have to clear the cache regularly. Disabling the plugin throws up missing tag errors. You have a $nolog global in the code but I can’t see where it’s actually used.
*Using debugging mode to disable caching would make things easy, but would one sacrifice being able to debug etc_cache at the same time?
TXP Builders – finely-crafted code, design and txp
Offline
Re: etc_cache: cache trying hard to be smart
jakob wrote #317102:
Is there a way of disabling caching globally temporarily, for example via a prefs switch or in debugging* mode?
Good idea, thanks, will think of it.
I’m currently doing flat theme updates to a site and have to clear the cache regularly. Disabling the plugin throws up missing tag errors. You have a $nolog global in the code but I can’t see where it’s actually used.
Well, if your cache update is properly configured, it should be refreshed automatically when something relevant changes, even in debug mode. But I’m not sure that lastmod
pref is updated in flat mode, so the cache update is probably not triggered. $nolog
serves just to suppress “false” visitors logging when etc_cache
“visits” its update page.
*Using debugging mode to disable caching would make things easy, but would one sacrifice being able to debug etc_cache at the same time?
I don’t remember whether etc_cache
has its proper debugger. Core-level errors display (syntaxes, etc) shouldn’t be impacted.
Offline
Re: etc_cache: cache trying hard to be smart
Hi Oleg,
I’ve come up against a situation where I think I need a special reset
/ filter
combination that I suspect etc_cache is capable of – you’ve made it so wonderfully flexible – but I’m stumped as to how to go about it.
I have a site with annual archives that are perfect for caching as they don’t change once the year is over. The current year, however, changes each week when a new article is published. How would I use reset
/ filter
so that the cache is refreshed whenever an article with a date in the current year is saved? Or – even better – when an article in the same year as the cached block is saved? Also useful would be able to additionally restrict the filter to articles saved in that year in a specific section.
TXP Builders – finely-crafted code, design and txp
Offline