Last Modified: 27.03.2023
Caching is a technology which enables to cache the results of rarely updated and resource consuming code areas (for example, those handling the database).
In case of a large database, a performance problem may occur due to the following reasons:
- Access to this information array for read or write results in competitive requests;
- The requests are fast in themselves, but there are so many of them that the database starts building a queue of them;
- The requests are slow and complicated, and they are also very frequent.
Multilevel caching is used precisely to relieve the most loaded places in terms of resources and time. Each caching technique may be used for each component separately by choosing an optimal option for a specific case.
Note: Until the developer decides on the caching strategy and on what they want to obtain from it, a blind activation of caching might bring no visible results.
If we take an Internet store as an example, then for each item of goods a file in cache memory will be created so that the server will not have to send requests to the database in case of any future queries of the buyer.
Caching is a process technique that permits caching the outputs of rarely updated and resource-intensive parts of the code (for example, those actively working with the database).
Two classes of caching are created to implement this:
- CPageCache - is a class for caching HTML result of script execution;
- CPHPCache - is a class for caching PHP variables and HTML result of script execution.
The Bitrix Framework system includes different caching techniques:
- Component caching (or Autocaching) – all dynamic components used to create web pages have an incorporated caching control support.
In order to use this technique it is sufficient to turn on autocaching using a button on the administrative panel. In this case, all of the components with activated autocaching mode will create cache and switch to a work mode without database queries.
- Uncontrolled caching is a possibility to set caching rules for resource-intensive parts of pages. Caching results are stored as files in the catalog /bitrix/cache/. If the caching time has not expired, instead of a resource-intensive code a preliminary created cache file will be connected.
Caching is called uncontrolled because cache is not rebuilt automatically after the modification of source data, but operates during the specified time after creation, which is set in the Component parameters.
The right use of caching permits to significantly increase the general performance of the site. However, it must be taken into account that an unreasonable use of caching may result in a serious increase of the /bitrix/cache/ catalog size.
- Controlled cache automatically updates component cache in case of data change.
- HTML cache should better be activated for a rarely changing section that is regularly visited by anonymous visitors. The technique is simple in operation, does not require that the user track changes, protects with a disc quota from data overload and autorecover operability in case the quota is exceeded or the data are changed.
- Menu caching. A special algorithm is used for menu caching that takes into account the fact that the majority of visitors are unregistered visitors.
Menu cache is controlled and updated during menu editing or a change of access rights to files and folders through an administrative interface and API.
Main caching settings are located on the page Caching settings (Control Panel > Settings > System settings > Cache Settings).
In the D7 core, the caching settings are set in a special file
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)):
"; print_r($arIBlockElement); echo "";
// write the preliminary buffered output to the cache file
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.
- If the current user authorization can affect the result of the cached code execution, the result of the CUser::GetUserGroupString function execution must be added to the identifier. This methos returns the string containing the current user groups ID.
- If you use pagewise navigation in the cached code, the CDBResult::NavStringForCache return value must be added to this identifier.
- It should be kept in mind that when using sorting, the sort order and values of variables containing the sorting field the must be included in this identifier
An example of caching with the CPHPCache class:
$cntIBLOCK_List = 10;
$cache = new CPHPCache();
$cache_time = 3600;
$cache_id = 'arIBlockListID'.$cntIBLOCK_List;
$cache_path = '/arIBlockListID/';
if ($cache_time > 0 && $cache->InitCache($cache_time, $cache_id, $cache_path))
$res = $cache->GetVars();
if (is_array($res["arIBlockListID"]) && (count($res["arIBlockListID"]) > 0))
$arIBlockListID = $res["arIBlockListID"];
$res = CIBlock::GetList(
'TYPE' => 'catalog',
'SITE_ID' => SITE_ID,
'ACTIVE' => 'Y',
"CNT_ACTIVE" => "Y",
"!CODE" => 'test%'
while($ar_res = $res->Fetch())
if($ar_res['ELEMENT_CNT'] > 0)
$arIBlockListID = $ar_res['ID'];
//////////// end cache /////////
if ($cache_time > 0)
$cache->StartDataCache($cache_time, $cache_id, $cache_path);
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.
Note: 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.