May 21, 2013

f Comment

Yii CFileCache: I Cannot Find the Key in the Caches. Why?

Amazon When you use system.caching.CFileCache to cache a big array of big objects in the web application in the following manner, the objects are usually stored as files under protected/runtime/cache/.

Yii::app()->cache->set('allItems', items);
Suppose the key you use in Yii::app()->cache->set() is allItems and the value you store is items. You search for allItems in each file under protected/runtime/cache/ and surprisingly you cannot find it!

This fact confuses you because if the key does not exist in the cache how does Yii find the correct value (items in this case) when you run Yii::app()->cache->get('allItems')?

Here's the content of an example cache in which I store an array of PHP objects of class Item.

a:2:{i:0;a:296:{i:1;O:4:"Item":16:{s:5:"score";N;s:6:"isMock";b:0;s:4:"body";N;s:10:"brandTitle";N;s:18:"
...

Read on to find the answer.

Solution
The answer is easy. Yii uses some hash algorithm to hash the key. That's why you cannot find the exact key in the cache. However you can find the exact value in the cache. In the above example you can see the clear text of the value.

So basically when you do Yii::app()->cache->get('allItems') Yii hashes allItems and finds out which cache stores the value for allItems. Yii finds the cache and extracts the value and returns it.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael