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.
Yii 1.0 or above
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());
Total 1 comment
It's also possible to use CExpressionDependency to get a memcache powered cache dependency:
Leave a comment
Please login to leave your comment.