A few questions about the cache

Hi all, just a have a few questions regarding the caching (APC) in Yii.

  1. Is there a way to list the cached items?

  2. Is there a way to flush some but not all of the cached item?

  3. How do I flush the entire cache?


  1. $cache = Yii::app()->cache;

    CVarDumper::dump($cache, 10, true);

  2. if you store the content in an array, you can flush some but not all.

  3. Yii::app()->cache->destroy();

Thank you!

Great to get such a quick response! Especially since it’s 5 am here :D.

I didn’t know about CVarDumper::dump, I think I’m going to get lots of use out of that.

Take a look at the API for all methods and properties. As you can see from the flush() method signature, it’s currently not possible to flush certain entries (e.g. via prefix). Such a feature will be implemented in the future.

Thanks for the reply.

Is it possible to have two separate caches (preferably two of the same, i.e. two APC caches in my case), so that I can clear one without clearing the other?

Maybe this could be done by using a prefix to the caches?

I found that this will return the APC cache store. The Yii keys are all MD5’ed though, so it’s hard to see which is which, but it’s a start. I can then just loop through and delete the ones I want to flush.

$cache = apc_cache_info('user');

CVarDumper::dump($cache['cache_list'], 10, true);

I don’t see why a MD5 is needed, because the keys are all prefixed with the application id anyways, so if you’re trying to prevent collisions (“generateUniqueKey()”) the same application saving the same key value will still collide, whether it is MD5’ed or not.

Removing the MD5, I can easily loop through and do what I want. I’ve manually done this now, but it would be nice to have an option to disable the MD5’ing.

For example, I’ll just look for the keys starting with “{AppID}Yii.COutputCache.{Page}…”

Here’s the function I am using to clear all values that match the OutputCache id ($controller). It returns the number of values cleared from cache.

Remember you have to remove the MD5 from CCache->generateUniqueKey() for this to work.

public function clearCache($controller)


	$key = Yii::app()->getId() . 'Yii.COutputCache.' . $controller . '..';

	$len = strlen($key);

	$cache = apc_cache_info('user');

	$count = 0;

	foreach ($cache['cache_list'] as $c)


		if (substr($c['info'], 0, $len) === $key)






	return $count;