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());
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'));
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.