Views: 10841
Last Modified: 18.09.2014

Starting from the main module of version 9.1.0, cache tags are supported. Cache can be marked with tags and reset also by tags. The cache of the infoblock components can be reset when the information contained in them changes.

Note. For large data amount that are often updated, tagged caching is not justified.

Cache Tagging Base Code:

01: $cache_id = md5(serialize($arParams));
02: $cache_dir = "/tagged_getlist";
03:
04: $obCache = new CPHPCache;
05: if($obCache->InitCache(36000, $cache_id, $cache_dir))
06: {
07:     $arElements = $obCache->GetVars();
08: }
09: elseif(CModule::IncludeModule("iblock") && $obCache->StartDataCache())
10: {
11:     $arElements = array();
12:     $rsElements = CIBlockElement::GetList($arParams["order"], $arParams["filter"]);
13:
14:     global $CACHE_MANAGER;
15:     $CACHE_MANAGER->StartTagCache($cache_dir);
16:     while($arElement = $rsElements->Fetch())
17:     {
18:         $CACHE_MANAGER->RegisterTag("iblock_id_".$arElement["ID"]);
19:         $arElements[] = $arElement;
20:     }
21:     $CACHE_MANAGER->RegisterTag("iblock_id_new");
22:     $CACHE_MANAGER->EndTagCache();
23:
24:     $obCache->EndDataCache($arIBlocks);
25: }
26: else
27: {
28:     $arElements = array();
29: }

Line 01 initializes the unique cache file identifier. Then, the catalog is defined from /bitrix/cache where the cache files are stored with different values of $arParams. It is important that this path start from slash but not end with it. When using memcached or APC as the cache it will be of critical importance for the cache reset.

Lines 04-05 initialize the cached object. If the caching time is not expired, line 07 will be executed and we will obtain the data from the cache file.

The condition in line 09 will be true nearly always. Here, the module is connected and caching starts.

Line 12 provides for a database query. It is important that all of the parameters on which a selection result depends “participate” in the cache identifier ($cache_id).

In line 14, the access to the variable $CACHE_MANAGER. is set. This object will control tags.

Line 15 – all subsequently allocated tags will be bound to the catalog $cache_dir. When the cache is reset in one of them, the contents of this catalog will be deleted. StartTagCache may be used recursively. For example:

$CACHE_MANAGER->StartTagCache($cache_dir1);
    $CACHE_MANAGER->StartTagCache($cache_dir2);
        $CACHE_MANAGER->StartTagCache($cache_dir3);
        $CACHE_MANAGER->EndTagCache();
    $CACHE_MANAGER->EndTagCache();
$CACHE_MANAGER->EndTagCache();

It is important that the calls StartTagCache and EndTagCache are balanced. The object $CACHE_MANAGER creates and tracks the stack of cache catalogs. In this case, the tags allocated for the catalog $cache_dir3 бwill also be connected with $cache_dir2 and $cache_dir1. The tags allocated for cache_dir2 will be also connected with $cache_dir1.

Line 18 provides for tagging the cache by using the method RegisterTag. The body length may not exceed 100 characters. Tag duplicates are deleted automatically when the RegisterTag method is used.

Line 22 writes catalog tags to the database table. The count is one insert per tag.

Cache reset:

$CACHE_MANAGER->ClearByTag("iblock_id_7");

Infoblock Components

To launch the mechanism, a constant in the file dbconn.php must be defined.

define("BX_COMP_MANAGED_CACHE", true);

If the method StartResultCache is used, the entry will be retrieved by StartTagCache with a path to the component cache (depending on the page). If the method EndResultCache is used (which, in its turn, is retrieved from IncludeComponentTemplate) - by EndTagCache.

In the infoblock module CIBlockElement::GetList and CIBlockSection::GetList return the object of the CIBlockResult class.

The method Fetch/GetNext of this object will retrieve $CACHE_MANAGER->RegisterTag("iblock_id_".$res["IBLOCK_ID"]);. If the selection does not contain any elements, the value of the infoblock identifier will be retrieved from the filter.

Cache reset is called from the methods Add/Update/Delete for elements, sections, and infoblocks. When the properties are changed, for example, using SetPropertyValueCode there will be no flushing. In this case, the following code can be used to clear cache:

if(defined('BX_COMP_MANAGED_CACHE'))
   $GLOBALS['CACHE_MANAGER']->ClearByTag('iblock_id_'.$arParams['IBLOCK_ID']);

The use of this tagged cache mechanism is not recommended in case of the frequent update of elements or sections. On the other hand, it must be convenient for content managers: all changes are immediately displayed on the website.



Courses developed by Bitrix24