Yii 1.1: cachedependency

dependency based on the primary cache provider
4 followers

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.

Requirements

Yii 1.0 or above

Usage

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());

Resources

Total 1 comment

#11658 report it
tfotherby at 2013/01/25 07:07am
Another option is to use CExpressionDependency

It's also possible to use CExpressionDependency to get a memcache powered cache dependency:

$myDependency = new CExpressionDependency('Yii::app()->cache->get("key")');
$model = ExampleAR::model()->cache(60, $myDependency)->findByAttributes(array('attr1' => 'blah1', 'attr2' => 'blah2'));

Leave a comment

Please to leave your comment.

Create extension