V
V
Valera2018-06-20 13:33:04
Yii
Valera, 2018-06-20 13:33:04

How to list caches or cache tags in Yii2?

There are reports on reporting dates, for each date I create a cache.

Yii::$app->cache->set('report_'.$_GET['DATE_REPORT'].'', $sql, 3600); // ключ 'report_'.$_GET['DATE_REPORT'].''

or
Yii::$app->cache->set('report_'.$_GET['DATE_REPORT'].'', $sql, 3600, new TagDependency(['tags' => 'report_'.$_GET['DATE_REPORT'].''])); // ключ и тег 'report_'.$_GET['DATE_REPORT'].''

Users request reports for different dates. This creates caches with the corresponding keys.
For example, we have 4 caches for 06/01/2018, 06/10, 06/19, 06/20.
There is a functionality in which the user can change the data in the report for past dates.
Thus, if the user changes the report for 06/10/2018, then I need to delete the report cache for 06/10/2018 and for 06/19, 06/20, because the data is no longer relevant. But not for 01.06, because. the reporting date has not yet arrived.
How can I find out what cache keys (tags) already exist in order to correctly delete the necessary caches?
Another example, I write groups of data to the cache with a different tag:
Yii::$app->cache->set('user_42_profile', 'key1', 1000, new TagDependency(['tags' => 'report-123']));
Yii::$app->cache->set('user_42_stats', 'key2', 1000, new TagDependency(['tags' => 'report-123']));

Yii::$app->cache->set('admin_42_profile, 'key3', 1000, new TagDependency(['tags' => 'super-123']));
Yii::$app->cache->set('admin_42_stats', 'key4', 1000, new TagDependency(['tags' => 'super-123']));

How can I see what tags are there? (tag name)
This is necessary in order to delete the cache later on a specific tag:
TagDependency::invalidate(Yii::$app->cache, 'report-123');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-06-21
@webinar

Doesn't it bother you that you are not using the cache for its intended purpose? The cache is on the database. The cache should depend on the relevance of the data, and not on the desires of the user.
Relevance is checked at the time of access to data with a specific key. He shouldn't delete anything else. Something else when it is requested, it should understand whether it is relevant or not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question