YiiHaa Webdevelopment blog about Yii-framework and more

14Feb/122

CacheDependency, a real cache dependency

Using cache dependencies enables you to be in control of your cached data. Yii provides several dependency methods such as database, file and globalState.

They all work fine but when I use memcached as caching provider, I do not want Yii to check dependencies located on the hard drive everytime Yii::app()->cache->get() is called, that is why I created a new dependency based on the current caching provider which you can download HERE.

Everytime Yii checks dependencies for changes it performs get(...) on the default caching provider. CacheDependency uses a prefix 'cacheDependency-$dependencyName' to make sure it will not conflict with other cached keys.

if (!($data = Yii::app()->cache->get('key'))) {
$data = new Model();
Yii::app()->cache->set(
    'key',
    $data,
    new CacheDependency('myDependency')
);
}

In this case the dependency will be set on the cache key 'cacheDependency-myDependency'. When the cache needs to be reset, simply set the dependency value to a new value.

Yii::app()->cache->set(CacheDependency::buildCacheId('myDependency'), time());

Posted by martijn

Comments (2) Trackbacks (0)
  1. What’s up, after reading this awesome article i am also
    happy to share my knowledge here with friends.

  2. I believe that is among the so much important info for
    me. And i’m glad studying your article. However should statement on some
    normal things, The web site style is ideal, the articles is truly excellent : D.
    Excellent job, cheers


Leave a comment

No trackbacks yet.