Yii 2 cache component that uses native PHP files to store cache data so:
The preferred way to install this extension is through composer.
Either run
composer require "sergeymakinen/yii2-php-file-cache:^2.0"
or add
"sergeymakinen/yii2-php-file-cache": "^2.0"
to the require section of your composer.json
file.
Set the following Yii 2 configuration parameters:
[
'components' => [
'phpCache' => [
'class' => 'sergeymakinen\yii\phpfilecache\Cache',
],
],
]
And you can use it like any Yii 2 cache class:
Yii::$app->phpCache->set('foo', 'bar')
If you need to execute any PHP bootstrap code before you get a value from a cache, pass a sergeymakinen\yii\phpfilecache\ValueWithBootstrap
instance with the value and a PHP code (which can be multiline of course) as a string to set()
:
use sergeymakinen\yii\phpfilecache\ValueWithBootstrap;
Yii::$app->phpCache->set(
'foo',
new ValueWithBootstrap(
'bar',
'Yii::$app->params[\'fromCache\'] = true;'
)
);
Since version 1.1 you can also pass a Closure
instead of a PHP code:
use sergeymakinen\yii\phpfilecache\ValueWithBootstrap;
use yii\helpers\StringHelper;
Yii::$app->phpCache->set(
'foo',
new ValueWithBootstrap('bar', function () {
\Yii::$app->params['fromCache'] = true;
\Yii::$app->params['name'] = StringHelper::basename('/etc/config');
})
)
Comments