Performance improvement in Joomla with WINCACHE user cache to cache session

Posted: Mar 05, 2010  4 comments  

Average Rating

Tags
JoomlaPerformance
PHP

Share this Post

Now that we have WINCACHE 1.1 Beta release which supports user as well as session cache, I am going to tell you a way to integrate session cache with Joomla using WINCACHE. Joomla has the way of integrating session cache based on user cache implementation and that’s what I am going to explain today. Increasing performance of Joomla by enabling it’s user cache functionality using WINCACHE is explained here.

Joomla session code is modular enough and in order to enable session cache based on WINCACHE user cache, one needs to paste the below code in a file named wincache.php and place it at folder libraries joomla\session\storage. This folder path is relative to your Joomla root installation folder.

<?php
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
 
/**
* WINCACHE session storage handler for PHP
*
* @package        Joomla.Framework
* @subpackage    Session
* @since        1.5
* @see http://www.php.net/manual/en/function.session-set-save-handler.php
*/
class JSessionStorageWincache extends JSessionStorage
{
    /**
    * Constructor
    *
    * @access protected
    * @param array $options optional parameters
    */
    function __construct( $options = array() )
    {
        if (!$this->test()) {
            return JError::raiseError(404, "The wincache extension is not available");
        }
 
        parent::__construct($options);
    }
 
    /**
     * Open the SessionHandler backend.
     *
     * @access public
     * @param string $save_path     The path to the session object.
     * @param string $session_name  The name of the session.
     * @return boolean  True on success, false otherwise.
     */
    function open($save_path, $session_name)
    {
        return true;
    }
 
    /**
     * Close the SessionHandler backend.
     *
     * @access public
     * @return boolean  True on success, false otherwise.
     */
    function close()
    {
        return true;
    }
 
     /**
      * Read the data for a particular session identifier from the
      * SessionHandler backend.
      *
      * @access public
      * @param string $id  The session identifier.
      * @return string  The session data.
      */
    function read($id)
    {
        $sess_id = 'sess_'.$id;
        return (string) wincache_ucache_get($sess_id);
    }
 
    /**
     * Write session data to the SessionHandler backend.
     *
     * @access public
     * @param string $id            The session identifier.
     * @param string $session_data  The session data.
     * @return boolean  True on success, false otherwise.
     */
    function write($id, $session_data)
    {
        $sess_id = 'sess_'.$id;
        return wincache_ucache_set($sess_id, $session_data, ini_get("session.gc_maxlifetime"));
    }
 
    /**
      * Destroy the data for a particular session identifier in the
      * SessionHandler backend.
      *
      * @access public
      * @param string $id  The session identifier.
      * @return boolean  True on success, false otherwise.
      */
    function destroy($id)
    {
        $sess_id = 'sess_'.$id;
        return wincache_ucache_delete($sess_id);
    }
 
    /**
     * Garbage collect stale sessions from the SessionHandler backend.
     *
     * @access public
     * @param integer $maxlifetime  The maximum age of a session.
     * @return boolean  True on success, false otherwise.
     */
    function gc($maxlifetime)
    {
        return true;
    }
 
    /**
     * Test to see if the SessionHandler 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"));
    }
}

I am also attaching the file ‘wincache.php’ in the zipped format. One can download, unzip and copy wincache.php that comes with it to joomla\session\storage folder.

Restart IIS and you are done. Now go to administrator page of Joomla and click on ‘Global Configuration’->’System’. Under ‘Session Settings’ one can see ‘wincache’ as part of Cache Handler. Select it from the drop down list and save the configuration. Pictorial representation of steps are below.

After login as ‘admin’ you will see the below page:

image

Click on ‘Global Configuration’ and then click on ‘System’ sub-link and one will see:

image

Click ‘wincache’ from ‘Session Settings’ drop down. Now ‘Save’ the configuration. And yes, restart IIS and you are done.

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

Now you can see session getting stored in user cache.

In this blog post you learnt how to leverage WINCACHE session 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.

Attachment: wincache.zip