Welcome Guest, Not a member yet? Register   Sign In
Preventing sessions to be created for certain URL's (speed up things)
#1

[eluser]Patrick Savalle[/eluser]
I built a very simple custom session object that prevents Code-igniter from creating sessions for certain URL's. Our system serves widgets / buttons from code-igniter using a memcached, and while doing performance tests we noticed that these widgets (that can be embedded on other websites) caused CI to create a session everytime they are viewed somewhere else. This is totally unnecessary and destructive for performance. So I built a custom session that prevents CI from creating sessions for specific URL's.

At first perfomance was around 200 requests/sec. Changing the ci_sessions table into a MEMORY table, brought perfomance to 400 requests/sec. Installing the piece of code below brought perfomance up to around 600request/sec. In the end if we carefully tune the lighttpd server we will probably reach 1000requests/sec. which is approx the same as we got from static images on a standard Apache config.

If you see something wrong with my approach or code, please let me know, otherwise use it to your advantage. Don't forget to put in your own URL-patterns, these are for our own system.

The best solution of course is to have a seperate server serve these high load widgets, as we do now, sharing a memcached instance between CI and that other server.

/libraries/MY_Session.php

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* This class prevents widgets, images and badges from
* constructing a session thereby increasing performance.
*/
class MY_Session extends CI_Session
{
    /**
     * Session Constructor
     *
     * The constructor runs the session routines automatically
     * whenever the class is instantiated.
     */
    public function __construct($params = array())
    {
        foreach( array( '@/badge.*@i', '@/widget.*@i','@/image.*@i' ) as $exclude)
            if (preg_match($exclude, $_SERVER['REQUEST_URI'])>0)
                return;
        parent::__construct( $params );
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB