Bitrix Site Manager

CPHPCache::InitCache

bool
CPHPCache::InitCache(
 int cache_life_time,
 string cache_id,
 mixed init_dir = false
)

The method InitCache initializes a number of properties of the CPHPCache class instance. If the cache file is absent or expired, the function will return false, or true otherwise.

Parameters

ParameterDescription
cache_life_time Cache time to live, in seconds.
cache_id A unique cache ID. This ID must contain all the parameters which may affect the result of cached code execution.
init_dir The name of directory for which the cache is valid. If "/", the cache is valid for all site directories.  
Optional. Current directory is used by default. 

See Also

Example



<?
// 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...
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;
?>