Hotaru Docs


Hotaru smartCache

Hotaru's smartCache comes in two flavors. One for caching database queries and one for caching blocks of HTML code.

For database queries

The Smart Cache for database queries needs four parameters: on/off, database table, cache duration (in minutes), and the SQL query. Like the standard database cache, it stores queries and uses them until the cache expires. The difference here is that Hotaru runs a single query first to find out if the database table has been recently updated. If so, it ignores the cache.

$h->smartCache('on', 'categories', 60, $sql); // start using cache
// SQL queries here
$h->smartCache('off'); // stop using cache

An example of it in action is the category bar. The categories table is rarely updated, so with the Smart Cache the database queries involved in building the category bar only run once every 60 minutes, instead of on every pageview. Of course, if there is an update, that is reflected immediately because the cache is ignored.

Also worth knowing is that Hotaru checks all the tables once to see if they've been updated and caches those results in memory, so it doesn't need to check the same table for updates multiple times in one page view.

For HTML code blocks

Similarly, the Smart Cache can be used for blocks of HTML code, such as those used to make sidebar widgets. In this case, smartCache takes five parameters: on/off, database table, cache duration (in minutes), block of HTML code, and a label to append to the cache filename.

First, assume you don't need to build a cache file:

$need_cache = false;

Then check to see if the (posts) table has been updated in the last (60) minutes. If cache output is returned from the Smart Cache you can then display it or return it somewhere. Otherwise, we know there has been an update and need to rebuild the cache. Store all your HTML in a $output variable and send it to the Smart Cache along with a label (e.g. 'top_posts') to append to the name of the cached file.

// check for a cached version and use it if no recent update:
$output = $h->smartCache('html', 'posts', 60, '', 'top_posts');
if ($output) {
    return $output; // cached HTML
$output = // HTML CODE HERE
$h->smartCache('html', 'posts', 60, $output, 'top_posts'); // make or rewrite the cache file

An example of this in practice is the Posts Widget. If there is an update to the posts table, the cache is ignored, an updated Posts Widget is output and a new cache is made, which will be used until there's another update to the posts table or 60 minutes expires. This way, we get the benefits of caching with the comfort of always having the latest data.

If you don't need update the cache according to database updates, see HTML Cache.

Other caching options: Standard Database Cache | HTML Cache | Memory Cache

Getting StartedDesign and LayoutPlugin DevelopmentAdvanced TopicsFunction ReferenceTroubleshooting