Documentation

StartDataCache

bool
CPHPCache::StartDataCache(
 int TTL = false,
 string uniq_str = false,
 mixed initdir = false,
 array vars = array(),
 string basedir = "cache"
)

The method starts the buffered HTML output, or emits the cache content if it is not expired yet. If the cache file is expired the function returns true, or false otherwise. Non-static method.

Analog method is available in the new core: Bitrix\Main\Data\Cache::startDataCache.

Parameters

ParameterDescription Available fr om version
TTL cache lifetime, in seconds.
Optional. Cache lifetime pre-specified in the InitCache method is used by default.
uniq_str A unique cache ID. This ID must contain all the parameters which may affect the result of cached code execution.
Optional. The unique cache ID pre-specified in the CPHPCache::InitCache method is used by default.
initdir Directory that stores the component cache, relative to /bitrix/cache/. If "/", the cache is valid for all site directories.
Optional. The name of directory specified in the CPHPCache::InitCache method is used by default.
vars An array of variables which are to be cached. Array should be in the following format:
array(
 "VARIABLE NAME 1" => "VARIABLE NAME  1", 
 "VARIABLE NAME  2" => "VARIABLE NAME  2", 
 ...)
Direct writing into the cache file is performed by the CPHPCache::EndDataCache method.
Optional, default value - empty array.
basedir Base cache directory. Default value equals to cache value, i. e. all cache is saved into /BX_PERSONAL_ROOT/cache/, wh ere BX_PERSONAL_ROOT equals to 'bitrix' by default.

See Also

  • CPHPCache::EndDataCache
  • CDBResult::NavStringForCache

    Examples of use

    <?
    // create an object
    $obCache = new CPHPCache; 
    
    // caching time - 30 minutes
    $life_time = 30*60; 
    
    // form the cache ID according to all parameters 
    // which may affect the resulting HTML
    $cache_id = $ELEMENT_ID.$SECTION_ID.$USER->GetUserGroupString(); 
    
    // if the cache exists and is not expired, then
    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;
    ?>


  • © «Bitrix24», 2001-2024
    Up