PHP 5.3 Xcache Custom Session Handler

Xcache is a PHP opcode and variable memory cacher. That is, it caches the PHP script code itself to save execution time by not having to re-parse and re-interpret the script every time a page is requested.

Xcache also lets you store PHP variables in the memory cache, and these variables will persist between page requests (and sessions). This is similar to say Java which has an application scope, where you can define a static Hashmap that's available from any request and doesn't get destroyed when a request ends.

This is useful for storing data-structures that are otherwise expensive to build on every request, but I won't get into that here.

You can also use Xcache to take over from the default PHP file-handling session manager. There are two advantages in doing this:

  1. Potentially faster as Xcache stores variables in memory and doesn't need file I/O.
  2. Potentially faster as the default file-handling mechanism means one user cannot make concurrent requests to the server in the same session due to file locks (try opening two tabs to the same PHP application, have the first tab issue a request that causes the server to sleep for say 60 seconds, meanwhile try to access any other page on the same PHP application and you'll notice the request waits for the first to return).
However, storing your sessions in Xcache is also very, VERY, dangerous. Xcache stores everything in memory, and you get to decide how much memory to allocate to variable caching. If this maximum memory is exceeded, Xcache may try to dump some older variables, or may not accept new variables. This means if you have a lot of users, Xcache will fill up fast and you'll get to a stage where sessions stop working as expected. 

So you've been warned. Doing this is almost always a bad idea. If you still wanna go ahead though, just run the following function before calling session_open to change from the default file-based mechanism to Xcache:

public static function initXcacheSessionHandler() {
            $open = function($s, $n) {
                return true;    //if xcache is loaded, don't need to do anything to access it
            $read = function($id) {
                if(!xcache_isset($id)) return '';   //must return '' if no value, as per PHP docs
                else return xcache_get($id);
            $write = function($id, $data) {
                if(!$data) return false;
                xcache_set($id, $data, EXPIRY_IN_SECONDS);  //e.g. 3600 for 1-hour sessions 
                return true;
            $close = function() {
                return true;
            $destroy = function($id) {
            $gc = function($expire) {
                return true;
            session_set_save_handler($open, $close, $read, $write, $destroy, $gc);

This will only work in PHP5.3+ as local functions were not available in earlier versions, although it's easy enough to adapt by using global functions and passing in the function names as strings to session_set_save_handler.


  1. Have you test it on a production environement with a cms?
    Wordpress, Joomla or Drupal?

    I am interested in this because I have a server with xcache alerady installed and working fine, I and don't want to install memcached too just for memory-saved sessions... .
    Or what do you recommend?


Post a Comment

Popular posts from this blog

Wkhtmltopdf font and sizing issues

Import Google Contacts to Nokia PC Suite

Can't delete last blank page from Word