Performance improvement in Joomla with WINCACHE user cache to cache session

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.

4 Comments

  • This code is committed to Joomla SVN at http://joomlacode.org/gf/project/joomla/scmsvn/?action=browse&path=%2Fdevelopment%2Ftrunk%2Flibraries%2Fjoomla%2Fsession%2Fstorage%2F.

    Thanks,
    Don.

  • Great! Thank you for sharing the information! http://www.iadjustablebeds.com

  • Don, I did the above but in the Joomla backend it only showed up in the "cache" settings, not in the session settings.
    WHen I run the wincache.php and look at the session cache statistics, I get:

    The session cache is not enabled. To enable session cache set the session handler in php.ini to wincache, for example: session.save_handler=wincache.

    Clearly the default settings are not sufficient.

    Where can I find the best php.ini settings to run wincache?
    Maybe good to mention this in your article too?

    Bastiaan

  • Bastiaan, you are getting confused. The session cache described here uses internally WINCACHE user cache to store things. I mean session variables are also stored in the the user cache. This is the reason you do not need to set session.save_handler to WINCACHE. After this implementation you should be looking at cached variable in user cache not session cache.

    Now if you want Joomla to use WINCACHE as a session cache just setting session.save_handler to WINCACHE in your php.ini is not sufficient. This is because Joomla application code overrides this internally. So that also needs to be changed. Just search for session.save_handler in the Joomla application code.

    The PHP.INI which gets installed with NTS PHP MSI while running in IIS FastCGI mode is good for running WINCACHE. Other WINCACHE related parameters can be tweaked depending on your application. Since it is totally application based, no generic guidance can be given. It may not work in all the cases.

    Hope this helps.

Comments have been disabled for this content.