Caching

MV allows to cache data for faster performing. Code fragments got with a help of large number of SQL queries or big processing costs are usually being cached to speed up the page generation on the server. For example, a multi-level menu, long lists with complex connections between many SQL tables might be cached. As a result the code fragment is saved in the database, and it needs only one SQL request to get it, instead of many initial actions to generate it.

After the data was saved in cache, we begin to extract it during the generation of a web page. When cache data becomes not actual, cache cleaning starts and new data is added into cache again to be used in next page generations. MV does not cache data for certain period of time, that means the absence of cache lifetime, so the cache cleaning occurs by actions with data in admin panel. Also, each cached element has its own key, bound to one or several models of a project. Dropping of cache takes place when any action with any record of the linked model is applied.

To switch on the caching process set "true" for "EnableCache" option in "config/setup.php" file.

Caching of method's result

Suppose we have displayLeftCatalogMenu() method in model Catalogs. This method displays tree of categories and uses a lot of SQL request to build all nested branches. We need to cache the result of this method in a template to make page generation faster. We must drop the cache only when making any changes in Catalogs model. Let's modify template's code responsible for output of the catalog tree.

//Before caching
<ul id="catalog-menu-list">
    <? echo $mv -> catalogs -> displayLeftCatalogMenu(); ?>
</ul>

//After caching
<ul id="catalog-menu-list">
    <?
        if(!$mv -> cache -> findAndDisplay("catalog-menu"))
        {
            $html = $mv -> catalogs -> displayLeftCatalogMenu();
            echo $mv -> cache -> save("catalog-menu", $html, array("catalogs"));
        }
    ?>
</ul>

Caching of template fragment

In next sample we have a code fragment created by many models. It needs to cache this fragment and renew the cache data if there are any changes occurs in any related model.

//Before caching
<div id="bottom-block">
    <? echo $mv -> blocks -> display(); ?>
    <div class="catalog">
        <? echo $mv -> catalogs -> displayMainMenu(); ?>
    </div>
    <? echo $mv -> pages-> displayMenu(); ?>
</div>

//After caching
<? if(!$mv -> cache -> displayOrStart("footer")) { ?>
<div id="bottom-block">
    <? echo $mv -> blocks -> display(); ?>
    <div class="catalog">
        <? echo $mv -> catalogs -> displayMainMenu(); ?>
    </div>
    <? echo $mv -> pages-> displayMenu(); ?>
</div>
<? } $mv -> cache -> stopAndSave(array("blocks", "catalogs", "pages")); ?>

Important notes

  • You can track the efficiency of caching by number of SQL queries and page generation time. To see such report you need to set 1 value in "index.php" file in the root of your project, where $debug object is created, as described in Debug section. The report will be shown at the bottom of the page.
  • Cache data is stored in database tables named "cache" and "cache_clean", which can be cleared at any moment without damage for the project. Also, you can drop all cache with static method Cache :: cleanAll().
  • If there will be no arguments in methods save() and stopAndSave(), then MV will drop cache by current key after any action in with any record in admin panel.
  • There are method "cleanByKey($key)" and "cleanByModel($model)" in $mv -> cache object designed for cache clearing by key by model accordingly.
  • If to set the value of 'EnableCache' option to 'false', then saving and returning of all the cache data will be stopped.