Views: 14185
Last Modified: 18.10.2024

Caching is a technology which enables to cache the results of rarely updated and resource consuming code areas (for example, those handling the database).

Performance

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


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).

The following classes are created for implementing the caching:

  • Cache - final class for caching PHP variables and HTML result for executed script.
  • The old kernel has the following classes:
  • CPageCache - a class for caching HTML result of script execution;
  • CPHPCache - a class for caching PHP variables and HTML result of script execution.

The Bitrix Framework system includes different caching techniques:

  • Component caching (or Managed caching) – 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 managed caching using a button on the administrative panel. In this case, all of the components with activated managed caching 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/ or in arbitrary в 'root_directory' folder, specified in .settings.php. By default, file cache root folder — /bitrix/cache/. Starting from Main module version 24.100.0 it can be replaced with arbitrary folder - 'root_directory'.
    Core parameter setup, defined in .settings.php. 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).

Note: In the D7 core, the caching settings are set in a special file.

Blocking mode for caching system

What is caching "blocking mode"

Starting from main 24.0.0 caching has "blocking mode". This mode:

  • creates cache in a single flow only (for example, user hits cache)
  • remaining flows get deprecated cache value until a new value is generated
  • if there's no old value available, each flow generates data as usual

In some cases such approach significantly reduces load - see the load test example below.

Example of load test

"Blocking mode" operation time

Blocking mode is automatically triggered if at least a single condition is satisfied:

  • Cache expired: if cache has expired, but кеш устарел, но прошло не больше определенного времени с момента его истечения
  • Cache was deleted: if cache was deleted using a key, but less than 60 seconds have expired (time limit is configurable)

    // Deleting cache via key with legacy value saved
    $cache = Bitrix\Main\Data\Cache::createInstance(['actual_data' => false]);
    $cache->clean($key, $dir);
    
    // Creating cache with old keys supported
    $cache = Bitrix\Main\Data\Cache::createInstance(['actual_data' => false]);
    

For all cache queries to be in blocking mode, specify use_lock => true:

// Memcache
    'cache' => [
        'value' => [
            'type' => [
                'class_name' => '\\Bitrix\\Main\\Data\\CacheEngineMemcache',
                'extension' => 'memcache'
            ],

            'memcache' => ['host' => '127.0.0.1', 'port' => '11211',],
            'use_lock' => true,

            // 'servers' => [
            //    0 => ['host' => '127.0.0.1', 'port' => 11211, 'weight' => 1],
            //    1 => ['host' => '10.100.0.59', 'port' => 11211, 'weight' => 1],
            //    2 => ['host' => 'unix:///var/run/memcached/memcached.sock', 'port' => '0'],
            //],

            'sid' => 'bxMemcache'
        ]
    ],

Caching mechanism operation

Standard cache mechanism executes in the following cases:

  • cache created or removed without specified parameter ['actual_data' => false]
  • cache has expired for a longer period than the specified time ttl * $ttlMultiplier
  • cache cleared by tag or by folder
  • cache is cleared by key, but more than 60 seconds have expired since it was deleted

Note: by default, components support "blocking mode".

Related documentation

  • Class CPHPCache in Developer documentation
  • Class CPageCache in Developer documentation
  • Class Cache in D7 documentation



Courses developed by Bitrix24