Super easy universal Magento cache extension

Update: I added some cool new features like admin grid and cron job for deleting expired cache data. All the new features are labeled as “NEW!” in this post.
Inchoo Unicache is an extremely simple and useful little extension that you can use to cache any kind of text/string based data in Magento. It uses helpers to make it accessible anywhere: from template files to your own custom code.
While making the Inchoo Flicker Gallery extension I needed an easy way of caching the Flickr API responses because the Flickr API servers were somewhat slow and there was no need to make the API request every time user opens the gallery. There were several different API responses that I needed to cache in one database table so I needed a universal solution to cache all of them and thatβs how Inchoo Unicache extension was born.
If you ever need to use some third-part REST or any other type of API that requires you to wait for the response, this extension is a must have. Please share your thoughts about other use cases in the comments section below.
Features
- Uses helpers to be more accessible
- Add, edit or remove cached items at any time with one line of code
- Check if there is cached data for a specified item name
- Check if the cached content for a specific item name should be used
- Get or set only the cached item timeout
- Intuitive and easy to use
- NEW! Admin grid listing all the cached data
- NEW! “Clear Cache” button in admin grid
- NEW! “Delete Expired” button in admin grid and helper function
- NEW! Cron job for deleting expired cache data (turned on by default!)
How to install?
Download Inchoo Unicache extension files to your Magento root directory. You can find it on github at https://github.com/srka/Inchoo-Unicache or if you have any problems with github you can download it directly from here.
If you are logged in to your Magento back-end you have to log out and then log in again. Clearing the cache would also be a good idea.
How does it work?
Inchoo Unicache uses one database table to store all the cached data in a simple name – content structure. Every cached item has:
- Name
- Content
- Date and time of the last update
- Timeout (in hours)
Item name is a unique identifier that you can use to create, edit or remove the cached item using only one line of code.
To create a cached item all you need to do is call helper function like this:
Mage::helper(‘unicache’)->writeCache([ITEM-NAME], [ITEM-CONTENT], [TIMEOUT]);
Where [ITEM-NAME] is the name of the cached item, [ITEM-CONTENT] is the content you want to cache and [TIMEOUT] is the time in hours that specifies how long should the item be cached (this argument is optional with a default value of 24).
To read the cached data all you need to do is call another helper function like this:
$cachedData = Mage::helper(‘unicache’)->readCache([ITEM-NAME]);
Where [ITEM-NAME] is the same item name you used when creating the cached item.
To make it even easier to display the cached data anywhere on the site I added the support for layout XML updates. This means that you can include any cached data by adding a block element to your XML update file like this:
<block type="core/template" name="cached" template="inchoo/unicache/data.phtml">
<action method="setCacheName"><cache_name>[ITEM-NAME]</cache_name></action>
</block>
Check out the “inchoo/unicache/data.phtml” file to customize how you want to display the cached data.
Available functions
There are several useful functions included in the extension that you can use to manage your cached data.
Mage::helper(‘unicache’)->hasCache([ITEM-NAME]);
Check if there is a cached item with the specified [ITEM-NAME]. This function only checks if the cached item exists it does not check if it has expired. Function returns true if there is a cached item with the specified [ITEM-NAME] and false otherwise.
Mage::helper(‘unicache’)->getCacheItem([ITEM-NAME]);
This function will return the collection item for the cached item with the specified [ITEM-NAME]. If there is no cached with the specified name function will return NULL.
Mage::helper(‘unicache’)->cacheExpired([ITEM-NAME]);
Check if cached item with the specified [ITEM-NAME] has expired. Expired time is calculated by adding item timeout to the last updated time. If the expired time is greater than current time the function will return true, otherwise it will return true. If there is no cached item with the specified [ITEM-NAME] the function will return true.
Mage::helper(‘unicache’)->shouldUseCache([ITEM-NAME]);
Check if you should use the cached content for the specified [ITEM-NAME]. This function checks if the cached item exists and is not expired.
Mage::helper(‘unicache’)->writeCache([ITEM-NAME], [ITEM-CONTENT], [TIMEOUT]);
This function writes the cached item with the specified [ITEM-NAME]. If there is no item with the specified name function will create a new item, if there is an item with the specified name function will update the content and timeout (if specified) values. [TIMEOUT] argument is optional and has a default value of 24. This function does not return any value.
Mage::helper(‘unicache’)->readCache([ITEM-NAME]);
This function returns the cached content data for the specified [ITEM-NAME]. If the cached item with the specified name exists and is not expired it returns its cached content data, otherwise it returns false.
Mage::helper(‘unicache’)->deleteCache([ITEM-NAME]);
This function deletes the cached item with the specified [ITEM-NAME]. In this case [ITEM-NAME] is optional and if left unspecified all the cached items would be deleted (this will effectively clear the entire cache).
Mage::helper(‘unicache’)->cacheTimeout([ITEM-NAME]);
This function returns the cache timeout from the cached item specified with [ITEM-NAME]. If there is no cached item with the specified name function will return false.
Mage::helper(‘unicache’)->updateCacheTimeout([ITEM-NAME], [TIMEOUT]);
This function will update only the cache timeout for the cached item with the specified [ITEM-NAME].
Mage::helper(‘unicache’)->deleteExpired();
NEW! This function will delete all the expired cached data (if any) and return a string status message: “Expired cache data deleted.” or “There was no expired cache data.”
NEW! Cron job for deleting expired cache data
I added a cron job that deletes expired cache data every day at midnight. You can disable or change this option in config.xml
14 comments
Fatal error: Class ‘Inchoo_Unicache_Helper_Data’ not found in … mage.php 546
Thanks Ryan. I am glad to see you got to the end of the post π
Lil heads up about misspelling of title for last paragraph.. ‘
NEW! corn job’ π
I added some cool new features like admin grid and cron job for deleting expired cache data. All the new features are labeled as βNEW!β in this post.
Thanks for all the suggestions and I hope this little extension will be much more useful now.
Try using getResourceCollection instead of getCollection.
There`s an error on magento 1.7 community:
Fatal error: Call to a member function addFilter() on a non-object in /app/code/community/Inchoo/Unicache/Helper/Data.php on line 6
I have a setup script but the standard Magento .gitignore on github haven’t included it π I’ll fix that, thanks.
Great extension, but I know how to make it even better:
add setup script π
make Inchoo/Unicache/Model/* uppercased to make it work on case sensitive file systems π
deleteCache(): if left unspecified all the cached items would be deleted. It will lead to the warning message without cache_name. You can edit method declaration as public function deleteCache($cache_name = null).
Magento cache can use not only DB, but also apc, memcached, xcache. So now my choise is build-in cache.
You can add viewer for the stored data and your solution will gain +100 points π
Spoke too soon.. I do actually have an override that sets the cache key for every block so that I can invalidate on cms block save event, but here is how a block can be cached with no custom code:
Here is how I cache CMS blocks. No custom code needed:
Hmm, I don’t get it.
Mage::app()->saveCache(), Mage::app()->loadCache(), Mage::app()->removeCache() and Mage::app()->cleanCache() seem a even more accessible and still without overriding core files.
The block class to automatically display cached data is new though, even if I can’t think of a good use case at the moment that couldn’t be handled using regular html_block caching.
What are the advantages over Mage_Core_Model_Cache ?
Basically, Inchoo Unicache is lightweight, simple and easy to use and modify without overriding Magento core files. It’s stand alone and can be used anywhere through helper functions.
Cool, I was running into exactly the same problem whilst working on a Magento extension that connected to a third party SOAP API. I solved it on an extension basis in a very similar way but this would’ve been useful for that project π