yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

Cache functionality for multiSet is not optimal #19268

Open andrew-svirin opened 2 years ago

andrew-svirin commented 2 years ago

Hi,

It is impossible to pass multiple dependencies per each key.

https://github.com/yiisoft/yii2/blob/master/framework/caching/Cache.php#L291

My goal is to use certain dependency for each key. It it very often usage.

For example I need to have next:

<key_123> => <tags-models, tags-model-123>
<key_124> => <tags-models, tags-model-124>

but now I can only make

<key_123> => <tags-models>
<key_124> => <tags-models>
samdark commented 2 years ago

Do you mean somethinbg like the following?

$cache->multiSet(['key1' => 'val1', 'key2' => 'val2'], null, ['key1' => dependency1, 'key2' => dependency2]);
WinterSilence commented 2 years ago

@samdark TagDependency or ChainedDependency

andrew-svirin commented 2 years ago

@samdark yes. yes definitely. dependency expect to be an TagDependency object

WinterSilence commented 2 years ago

@samdark hold my tea (:

andrew-svirin commented 2 years ago

@winiarekk TagDependency or ChainedDependency are oriented to tag all values in multiSet() with same logic. When all values should be tagged by its own logic, then to these classes can not solve the problem.

WinterSilence commented 2 years ago

@andrew-svirin why you not store this complex cache in DB tables? you can update cache state by sheduler event(see CREATE EVENT ... ON SCHEDULE in mysql) or by crontab task.

andrew-svirin commented 2 years ago

@WinterSilence I using interface CacheInterface, that adds many benefits. And the main goal is to getMultiple values from Redis by one request and saveMultiple missing in cache values by one request. In other words getOrSetMultiple()

WinterSilence commented 2 years ago

@andrew-svirin this is where you should have started: "I'm use yii\redis\Cache bla-bla-bla..." because this class in other package

It's not correct using multiSet(), use set() in cycle to set dependency for every values or extend yii\redis\Cache::multiSet() to do this. If you explain current case/task more, we can try find optimal solution to do this.