K
K
Khurshed Abdujalil2017-05-22 06:57:03
Yii
Khurshed Abdujalil, 2017-05-22 06:57:03

How to flush Yii2 cache?

How to reset such caching?

$result = Customer::getDb()->cache(function ($db) use ($id) {
    return Customer::findOne($id);
}, 60 * 60 * 24 * 4);

I want to reset the cache when updating the record

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Khurshed Abdujalil, 2017-05-22
@akhur

$data = $cache->getOrSet('customer' . $id, function ($db) use ($id) {
    return Customer::findOne($id);
}, 60 * 60 * 24 * 4);

then removal
$cache->delete('customer' . $id);

M
Maxim Fedorov, 2017-05-22
@qonand

use yii\caching\DbDependency to determine if a record has changed

M
Maxim Timofeev, 2017-05-22
@webinar

flush()
www.yiiframework.com/doc-2.0/yii-caching-cache.htm...

K
Kar_mad_on, 2017-11-24
@Kar_mad_on

It's best to use Tags for this cache, so it's possible to do one cache flush for a whole bunch of keys and queries

$tag = 'Customer-' . $id;                           // готовим таг
  $ttl = 60 * 60 * 24 * 4;                            // готовим время жизни кеша

   $result = Customer::getDb()->cache(function ($db) use ($id) {
           return Customer::findOne($id);
   }, $ttl, new TagDependency(['tags' => $tag]));      // Пометка тагом данного кеша
                
 TagDependency::invalidate(Yii::$app->cache,$tag);   // Очистка всех кешей с данным тагом

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question