The last notes
All English-language materials have been translated fully automatically using the Google service
Preload files and classes using opcache.preloader in php 7.4+
In php, starting from version 7.4, it became possible to preload project files into memory. This can potentially significantly speed up access to them from project files. It works like this:
1. You write a file in which you list all the necessary files for preloading
2. Add the opcache.preload option to the php.ini file
3. Restart the httpd service
4. You work with files and classes preloaded into memory
5. Take into account that after making changes to files and classes, to update them in memory - the httpd service must be restarted.
In its simplest form, the listing of the preloader.php
file will look like this:
<?
require_once __DIR__.'/local/PATH_TO_VENDOR/vendor/autoload.php';
require_once __DIR__.'/local/PATH_TO_CUSTOM_CLASSES/include/autoload.php';
// File list
$files = [
//'/var/www/data/site/html/file_to_preload.php',
];
// Preload all root project files
foreach ($files as $file) {
require_once $file;
//Or
//opcache_compile_file($file);
}
Assuming that opcache
is already installed on your server and you have root
access to it, then go to the file /etc/php.d/10-opcache.ini
and add commands:
; Root User name. On BitrixVM it is usually root
opcache.preload_user=root
; Full path to your preloader file
opcache.preload=/home/bitrix/ext_www/site/preloader.php
Full listing opcache.ini
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=88
opcache.max_accelerated_files=200000
opcache.max_wasted_percentage=1
opcache.validate_timestamps=1
opcache.revalidate_freq=0
opcache.fast_shutdown=1
opcache.save_comments=1
opcache.load_comments=1
opcache.blacklist_filename=/etc/php.d/opcache*.blacklist
opcache.preload_user=root
opcache.preload=/home/bitrix/ext_www/site/preloader.php
Restart httpd
service with command
service httpd restart
And remember it - you will need it to apply changes to the preloaded classes
If you did everything correctly, then you can see that preloading works with the command:
print_r(opcache_get_status()['preload_statistics'])
It will print an array of all files and classes that were preloaded
Comments