Performance improvement in Joomla using WINCACHE user cache

Now that we have WINCACHE 1.1 Beta released which has got implementation for both user and session cache, one can easily take advantage of WINCACHE user cache and increase performance of Joomla. In this post I am going to tell you steps to use WINCACHE user cache with Joomla.

Joomla caching code is modular and in order to enable WINCACHE user cache, one needs to paste the below code in a file named wincache.php and place it at folder libraries\joomla\cache\storage. All the folders mentioned here is with respect to root folder of Joomla installation.

<?php
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
 
/**
 * WINCACHE cache storage handler
 */
class JCacheStorageWincache extends JCacheStorage
{
    /**
     * Constructor
     *
     * @access protected
     * @param array $options optional parameters
     */
    function __construct( $options = array() )
    {
        parent::__construct($options);
 
        $config        = & JFactory::getConfig();
        $this->_hash    = $config->getValue('config.secret');
    }
 
    /**
     * Get cached data from WINCACHE by id and group
     *
     * @access    public
     * @param    string    $id        The cache data id
     * @param    string    $group        The cache data group
     * @param    boolean    $checkTime    True to verify cache time expiration threshold
     * @return    mixed    Boolean false on failure or a cached data string
     * @since    1.5
     */
    function get($id, $group, $checkTime)
    {
        $cache_id = $this->_getCacheId($id, $group);
        $this->_setExpire($cache_id);
        return wincache_ucache_get($cache_id);
    }
 
    /**
     * Store the data to WINCACHE by id and group
     *
     * @access    public
     * @param    string    $id    The cache data id
     * @param    string    $group    The cache data group
     * @param    string    $data    The data to store in cache
     * @return    boolean    True on success, false otherwise
     * @since    1.5
     */
    function store($id, $group, $data)
    {
        $cache_id = $this->_getCacheId($id, $group);
        wincache_ucache_set($cache_id.'_expire', time());
        return wincache_ucache_set($cache_id, $data, $this->_lifetime);
    }
 
    /**
     * Remove a cached data entry by id and group
     *
     * @access    public
     * @param    string    $id        The cache data id
     * @param    string    $group    The cache data group
     * @return    boolean    True on success, false otherwise
     * @since    1.5
     */
    function remove($id, $group)
    {
        $cache_id = $this->_getCacheId($id, $group);
        wincache_ucache_delete($cache_id.'_expire');
        return wincache_ucache_delete($cache_id);
    }
 
    /**
     * Clean cache for a group given a mode.
     *
     * group mode        : cleans all cache in the group
     * notgroup mode    : cleans all cache not in the group
     *
     * @access    public
     * @param    string    $group    The cache data group
     * @param    string    $mode    The mode for cleaning cache [group|notgroup]
     * @return    boolean    True on success, false otherwise
     * @since    1.5
     */
    function clean($group, $mode)
    {
        return true;
    }
 
    /**
     * Test to see if the cache storage is available.
     *
     * @static
     * @access public
     * @return boolean  True on success, false otherwise.
     */
    function test()
    {
        return (extension_loaded('wincache') && function_exists('wincache_ucache_get') && !strcmp(ini_get('wincache.ucenabled'), "1"));
    }
 
    /**
     * Set expire time on each call since memcache sets it on cache creation.
     *
     * @access private
     *
     * @param string  $key   Cache key to expire.
     * @param integer $lifetime  Lifetime of the data in seconds.
     */
    function _setExpire($key)
    {
        $lifetime    = $this->_lifetime;
        $expire        = wincache_ucache_get($key.'_expire');
 
        // set prune period
        if ($expire + $lifetime < time()) {
            wincache_ucache_delete($key);
            wincache_ucache_delete($key.'_expire');
        } else {
            wincache_ucache_set($key.'_expire',  time());
        }
    }
 
    /**
     * Get a cache_id string from an id/group pair
     *
     * @access    private
     * @param    string    $id        The cache data id
     * @param    string    $group    The cache data group
     * @return    string    The cache_id string
     * @since    1.5
     */
    function _getCacheId($id, $group)
    {
        $name    = md5($this->_application.'-'.$id.'-'.$this->_hash.'-'.$this->_language);
        return 'cache_'.$group.'-'.$name;
    }
}

I am also attaching the file named ‘wincache.php’ in zipped format which can be unzipped and wincache.php can be directly copied to libraries\joomla\cache\storage folder.

Restart IIS and go to administrator page of Joomla and click on ‘Global Configuration’->’System’. Under ‘Cache Settings’ one can see ‘wincache’ as part of Cache Handler. Select it from the drop down list and save the configuration. Pictorial representation below after login to control panel using admin password.

After you are logged in as administrator one will see below page:

image

Click on ‘Global Configuration’ icon:

image

Click next on ‘System’ tab and then click on drop down ‘Cache Settings’ as shown below:

image

Select ‘wincache’ from drop down. Also click on radio button ‘Yes’ for Cache label.

Save the settings and restart IIS.

In order to verify that WINCACAHE is storing values in user cache, hit the homepage of your Joomla installation. Now open WINCACHE.PHP file which ships with the installation and click on tab named “User and Session Cache”. It should look something like below:

image

In this blog post you learnt how to leverage WINCACHE user cache functionality to enhance performance of Joomla. Hopefully this will be useful to you in running Joomla faster on WINDOWS. Happy caching and till we meet again ‘Good Bye’.

Thanks,

Don.

PS: This works only with WINCACHE version 1.1.0 and higher. So please make sure that you are not running any lower versions of WINCACHE like 1.0.0 or 1.0.1.

5 Comments

  • Hi Klas,

    Thanks you for the suggestion and taking time in writing comment here. Very much appreciated. I will definitely look at your implementation.

    To answer your question, WINCACHE does do garbage collection periodically or when certain user cache functions are called. So items which have expired in the cache are removed. You can see the implementation of it at http://svn.php.net/viewvc/pecl/wincache/trunk/wincache_zvcache.c?view=markup. Look for function named 'run_zvcache_scavenger'.

    Again thank you.

    Don.

  • This code is now part of Joomla development branch and will be available in Joomla 1.6 release. The code can be seen at http://joomlacode.org/gf/project/joomla/scmsvn/?action=browse&path=%2Fdevelopment%2Ftrunk%2Flibraries%2Fjoomla%2Fcache%2Fstorage%2F. A big thanks to Joomla developers for doing this.

    Regards,
    Don.

  • Anonynous comments on this blog post are peroidically removed. So you will need to login and post comments so that it is here permanently.

    Thanks,
    Don.

  • Ok, did that, it just seems funny with you answer to unexisting comment.

    Sent you an email about this topic, I hope you received it (from klas dot berlic at gmail dot com)

  • Thank you for sharing the information! http://www.astrabeds.com/accessories/adjustables/

Comments have been disabled for this content.