Bitrix: working with composite cache.
Rus
Eng
Битрикс: работа с композитным кешем.
I solve complex technical challenges for Bitrix and 1C—ranging from auditing existing code to website development and configuring complex integrations—with a guarantee of results.
Recommended

Website hosting

If you are choosing hosting for your project, I recommend Timeweb. Modern infrastructure, a wide range of plans from starter options to high-load VPS, and responsive tech support that actually helps instead of sending template replies. I host some of my own projects there and recommend it to my clients.

Go to site Affiliate link. The price remains the same for you.

All English-language materials have been translated fully automatically using the Google service

First, go to /bitrix/admin/composite.php?lang=en and open the Settings item. These settings will be enough:

Composite mode settings

Turn on logging

 define ("BX_COMPOSITE_DEBUG", true);
define ("LOG_FILENAME", $ _SERVER ["DOCUMENT_ROOT"]. "/ log.txt"); 

Dynamic content outside of a component, for example in a template

\Bitrix\Main\Page\Frame::getInstance()->startDynamicWithID("dynamic-area");

\Bitrix\Main\Page\Frame::getInstance()->finishDynamicWithID("dynamic-area", "Загрузка...");

Dynamic buffered content

$frame = new \Bitrix\Main\Page\FrameHelper("dynamic-area");
$frame->begin();
   //динамический контент
$frame->beginStub();
  //заглушка
$frame->end();

Dynamic area with browser cache. Load statics from the last hit

 $ frame = $this->createFrame('dynamic-area')->begin('');
$ frame-> setBrowserStorage (true);
// Content
$ frame-> end (); 

Dynamic area with spawn animation

 $ frame = $this->createFrame('dynamic-area')->begin('');
$ frame-> setAnimation (true);
 // Content
$ frame-> end (); 

Partial component caching. For example, when showing the current price

$frame = $this->createFrame('price-index', false)->begin()		
	echo $price;
$frame->beginStub()
	echo 'руб.';			
$frame->end()

Disable Composite Caching

\Bitrix\Main\Data\StaticHtmlCache::getInstance()->markNonCacheable();

Removing Composite Cache via API

$staticHtmlCache = \Bitrix\Main\Data\StaticHtmlCache::getInstance();

$staticHtmlCache->deleteAll();

Composite and Region Dependent Content

In the init.php file, override the method that generates the file name in the composite cache.

class CacheProvider extends Bitrix\Main\Data\StaticCacheProvider
{
public static function createKey()
{
	global $USER;
	$page_name = "page";
	if(!empty($_SESSION["CITY_ID"]))
		$page_name .= "_region_".$_SESSION["CITY_ID"];

	return $page_name;
}
public function setUserPrivateKey(){}
public function isCacheable()
{
	return true;
}
public function getCachePrivateKey()
{
	return self::createKey();
}
public function onBeforeEndBufferContent(){}
}

In the city definition component (for example articul.geolocation.detect_ip / component.php ) write the cookie BITRIX_SM_PK

setcookie("BITRIX_SM_PK", 'page_region_'.$_SESSION["CITY_ID"], $cookie_life, "/", constant("COOKIE_DOMAIN"));

In the file /local/php_interface/composite_first_start_cookie_fix.php we write the cookie BITRIX_SM_PK so that the composite will also work out on the first visit (we include this file BEFORE header. php on composite pages):

Original solution here

 

Worth reading:

Aleksandr Tichiy
Author

Aleksandr Tichiy

Fullstack developer · 1C-Bitrix

I build and modernize online stores and corporate portals on 1C-Bitrix. Integrations with 1C, high-load performance, 152-FZ compliance. Writing about architecture and e-commerce.


Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Please message me to discuss the task. I work remotely and officially (as a self-employed professional); my hourly rate is 1,800 RUB, and I can dedicate 3–4 hours a day to the project. I respond quickly, and the consultation is free.

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site