A
A
Andrey2015-11-25 09:05:14
Yii
Andrey, 2015-11-25 09:05:14

Yii2 how to set custom yii\caching\Cache\buildKey for a module?

Hello!
There is an idea to use pseudo-tags in caching at the level of cache keys, like:
['|blog|', '|user|', '|userstat|', '|commentcount|', '|uid=777|']
so that if necessary the entire cache could be broken as needed fragmentarily. For example, if a user commented on something, they cleared him and only his cache of statistics, latest comments, all comments, and various other useful garbage.
but, the regular buildKey breaks this topic with its
md5 (json_encode ($ key))
is there a way for a separate module to override only this method?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2015-11-25
@VladimirAndreev

In general, it seems to work like this:
In the module initialization file, write like this:

if (!empty($this->components['cache'])) {
    yii::$container->set(
        'cache',
        ['class' => $this->components['cache']['class']]
    );
}

When you get a cache instance, call it like this:
The only thing that is not yet clear is whether it is possible to dynamically determine the cache type, since while app\modules\blog\Cache inherits from yii\caching\MemCache ...
The only option that is visible so far is to put buildKey into a trait and create all available cache class variants...
So it's possible to use more than one cache engine in a module :)

V
Vit, 2015-11-25
@fornit1917

In Yii2, all components are created through a DI container. You can slip your Cache implementation into the container in the config of the module you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question