Magento: Super easy universal cache extension

June 5th, 2014 by in Backend, Database, Development, Magento 0
srka-magento-unicache
Note: This is a new version of the Inchoo Unicache extension. The old extension will not be updated anymore.

 

Srka Magento 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 from anywhere; from template files to your own custom code.

 

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 can cache those responses for any specified number of hours significantly improving your code performance.

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
  • Admin grid listing all the cached data
  • “Clear Cache” button in admin grid
  • “Delete Expired” button in admin grid and helper function
  • Cron job for deleting expired cache data (turned on by default!)

How to install?

Download Srka Magento Unicache extension from GitHub (https://github.com/srka/Srka-Magento-Unicache) to your Magento root directory.

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?

Srka Magento 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="srka/unicache/data.phtml">
        <action method="setCacheName"><cache_name>[ITEM-NAME]</cache_name></action>
</block>

Check out the “srka/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 a collection item for the cached item with the specified [ITEM-NAME]. If there is no cached item 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();

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.”

Cron job for deleting expired cache data

There is a cron job that deletes expired cache data every day at midnight. You can disable or change this option in config.xml

Written by Srdjan Stojiljkovic

Srdjan is a Frontend Developer with a passion for pixel perfect design and great user interfaces. He also designed and developed this site and is dedicated to provide best and most useful articles about development in general.


See all articles from this author »

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>