Some CCache Improvements, flush only specific values

This is a follow up on this conversation (specifically my last two entries):

http://www.yiiframework.com/forum/index.php?/topic/15368-a-few-questions-about-the-cache/page__view__findpost__p__76458

So yeah, here’s how I use my Cache, and it allows me to clear some cached values without clearing all. This is because I have some pages that render sort of quickly and are updated every 5 minutes, while others render slower, but are only updated once a day. I need to keep the “24 hour” cached items while flushing the “5 min” items every 5 minutes.

I also can’t use the expire after 5 minutes option because of a rebuild of the database table every update, so if they cache at separate times the sorted values in my gridview could display older or newer values depending on the sort key.

Well, that’s my rationale, and here’s my code suggestion.




class MyApcCache extends CApcCache

{

	public function init()

	{

		parent::init();

	}


	protected function generateUniqueKey($key)

	{

		return ($this->keyPrefix.$key);

	}


	public function flushValues($search = '')

	{

		if (empty($search))

			return apc_clear_cache('user');


		$cache = apc_cache_info('user');

		$count = 0;


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

		{

			if (strpos($c['info'], $search) !== false)

			{

				apc_delete($c['info']);

				$count++;

			}

		}


		return $count;

	}

}



As for the MD5 issue in generateUniqueKey(), I really think it should be removed because it only serves to obfuscate the key (which prevents flushing only some values) and doesn’t add any uniqueness to the key (in fact, it adds a very slight chance of a collision).

Check out this ticket: http://code.google.com/p/yii/issues/detail?id=490