Caching is a technology which enables to cache the results of rarely updated and resource consuming code areas (for example, those handling the database).
The following two classes are used to implement this:
Caching results are stored as files in the directory /bitrix/cache/. If the cache is not expired, a preliminary created caching file is included instead of the resource consuming code.
The correct use of cache allows to greatly increase the total site efficiency. However, it should be taken into account that unwise use of caching may bring about a serious grow of the /bitrix/cache/ directory size.
<? // create an object $obCache = new CPageCache; // caching time - 30 minutes $life_time = 30*60; // form the cache ID according to all // which may affect the resulting HTML $cache_id = $ELEMENT_ID.$IBLOCK_TYPE.$USER->GetUserGroupString(); // initialise buffered output if($obCache->StartDataCache($life_time, $cache_id, "/")): // obtain the information block element parameters from the database if($arIBlockElement = GetIBlockElement($ELEMENT_ID, $IBLOCK_TYPE)): echo "<pre>"; print_r($arIBlockElement); echo "</pre>"; endif; // write the preliminary buffered output to the cache file $obCache->EndDataCache(); endif; ?>
In this example, the method CPageCache::StartDataCache checks the existence of the cache file which is valid and not expired. If such file exists, it is included and displayed, otherwise buffering is turned on. The buffered output is written to the cache file using the method CPageCache::EndDataCache.
The first parameter of the method CPageCache::StartDataCache specifies the time interval, in seconds, during which the cache file is valid and is not expired (the interval is counted from the cache file create time).
The second parameter contains the unique identifier of the cache
instance. It should be mentioned that if any variables may affect the
result of the cached code execution, their values are to be included in this identifier.
<? // create an object $obCache = new CPHPCache; // caching time - 30 minutes $life_time = 30*60; // form the cache ID according to all // which may affect the resulting HTML $cache_id = $ELEMENT_ID.$SECTION_ID.$USER->GetUserGroupString(); // if the cache exists and is not expired... if($obCache->InitCache($life_time, $cache_id, "/") : // obtain cached variables $vars = $obCache->GetVars(); $SECTION_TITLE = $vars["SECTION_TITLE"]; else : // otherwise call the database $arSection = GetIBlockSection($SECTION_ID); $SECTION_TITLE = $arSection["NAME"]; endif; // add a menu item to the navigation chain $APPLICATION->AddChainItem($SECTION_TITLE, $SECTION_URL."SECTION_ID=".$SECTION_ID); // start buffered output if($obCache->StartDataCache()): // obtain the information block element parameters from the database if($arIBlockElement = GetIBlockElement($ELEMENT_ID, $IBLOCK_TYPE)): echo "<pre>"; print_r($arIBlockElement); echo "</pre>"; endif; // write the preliminary buffered output to the cache file // together with an additional variable $obCache->EndDataCache(array( "SECTION_TITLE" => $SECTION_TITLE )); endif; ?>
This example differs from the previous in caching the PHP variable $SECTION_TITLE in addition to the HTML code. Structure and type of the cached PHP variables is arbitrary. The method CPHPCache::InitCache checks if the unexpired and valid cache file exists. If this file is found, it is included; all the cached variables are available after the method CPHPCache::GetVars returns. The method CPHPCache::EndDataCache writes PHP variables and the buffered HTML code to the cache file.
To disable caching for a single page, you should authorize with administrative rights and open this page with the parameter &clear_cache=Y. Calling a page with parameter &clear_cache_session=Y after authorization will disable caching for all the session pages. Cache files can be deleted in the administrative section on the settings page of the Kernel module.
|© 2001-2005 Bitrix||Bitrix Site Manager - Content Management & Portal Solutions|