1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Docs] Hotaru's Different Caching Methods

Discussion in 'Developing Plugins' started by Nick, Nov 27, 2009.

  1. Nick

    Nick Well-Known Member

    Hotaru CMS uses different kinds of caching for different things. Here's an overview of each of them so you can use them appropriately in your plugins.

    1. The standard database cache

    In Admin->Settings, you can choose to enable or disable the database cache and set its duration. By default this cache lasts for 12 hours and is mostly used for caching the very frequent plugin hook checks.

    $h->db->cache_queries true;    // start using cache
    // SQL queries here
    $h->db->cache_queries false;    // stop using cache
    2. The "Smart Cache"

    Hotaru's Smart Cache 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:

    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) {
    $output// cached HTML
    } else {
    $need_cache true;
    $output // HTML CODE HERE
    if ($need_cache) {
    $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.

    3. The memory cache

    When a page is loaded, it's quite common for the same database queries to be repeated. The memory cache is an associative array that is used to store the first round of SQL queries and their results so that they don't need to be repeated.

    Here's how it's used in the getPost function:

         * Gets a single post from the database
         * @return array|false
    public function getPost($h$post_id 0)
    // Build SQL
    $query "SELECT * FROM " TABLE_POSTS " WHERE post_id = %d ORDER BY post_date DESC";
    $sql $h->db->prepare($query$post_id);
    // Create temp cache array
    if (!isset($h->vars['tempPostCache'])) { $h->vars['tempPostCache'] = array(); }
    // If this query has already been read once this page load, we should have it in memory...
    if (array_key_exists($sql$h->vars['tempPostCache'])) {
    // Fetch from memory
    $post $h->vars['tempPostCache'][$sql];
            } else {
    // Fetch from database
    $post $h->db->get_row($sql);
    $h->vars['tempPostCache'][$sql] = $post;
            if (
    $post) { return $post; } else { return false; }
    Last edited: Feb 20, 2010

Share This Page