Memcached support

I'm working hard lately. The project has been on hiatus for a long time (2009 and 2010), and only now "I put the batteries" and have added significant improvements to the Project.

One of these improvements is Memcached support.

Complicated?
Not, Implement Memcached support in Smarty Cache Handler Function
is easier than you think.

Following the Smarty documentation, I've created a Cache Handler Function to use Memcached, from 0, without following any other documentation or third party code.

In "config.php" will declare the Smarty Cache Handler Function depending on selected options

config.php

<?php
// :: Output ommited

 
if(!empty($memcached_server)){
    if(
class_exists('Memcached')){ // Use the Class Memcache or Memcached; see bellow
     
$memcached = new Memcached('dpcms_ '$site_id);
      if(
$memcached->addServer($memcached_server$memcached_port)){
       
$smarty->cache_handler_func 'cache_handler_memcached';
      }
    }
  }

// :: Output ommited

?>

And, in the Functions directory, the "memcached.php" script has the Function to use the Smarty Cache Handler Fucntion. As you see, is really easy to Read and Write the Cache into Memcached daemon. Of course, you can use this function to your own project using Smarty, too.

memcached.php

  <?php
  
    ################################################
    #                                              #
    #    DPortal CMS, CMS without Database engine  #
    #                                              #
    #  Memcached support module (memcached.php)    #
    #                                              #
    #  Copyright (c) Davod.                        #
    #                                              #
    #  This program is published under the         #
    #  GNU general Public License                  #
    #                                              #
    #  Please see README and LICENSE for details   #
    #                                              #
    ################################################

function cache_handler_memcached($action,
                                 &$smarty,
                                 &$cache_content,
                                 $tpl_file=null,
                                
$cache_id=null,
                                 $compile_id=null,
                                 $exp_time=null){

 
// Class Memcached previouslly constructed.
  global $memcached;
 
global $site_id;

 
$CacheID 'dcms_' $site_id '_' sha1($tpl_file.$cache_id.$compile_id);

  switch(
$action){
    case 
'read' :
       // Get the contents is easy than query a DB
      $content $memcached->get($CacheID);
            
     
if(!empty($content)) return $content;
      else return 
false;
      break;
        
    case 
'write' :
      // here is the same, but to Write
      if($memcached->set($CacheID,$cache_content)) return true;
     
else return false;        
      break;
        
    case 
'clear' :
      if(!empty(
$CacheID)){
        if(
$memcached->delete($CacheID,0)) return true;
        else return 
false;
     
}else{
        // Two alternatives to clear the Cache, using Memcached::Flush,
        // or a WHILE block to delete all the cache keys
        if(
$memcached->flush(0)) return true;
        else return 
false;        
      }

      break;
        
  default:
        
     
// error, unknown action
     
$smarty_obj->_trigger_error_msg 
      ("cache_handler: unknown action \"$action\"");
     
$return false;
      break;
  }
}

?>

Only a little dilemma in Clear cache mode: Use Memcached::flush() to invalidate all the Cache contents (and if contents of the key will be empty, Function will return FALSE and the Cache object should be recreated), or get the list of all the keys stored in memcached, filter, and use a WHILE block to delete one-to-one key.

Note: PHP has two classes to interact with the Memcached daemon: Memchace and Memcached. The two do the same, but both have few differences: Memcached is newer than Memcache (2009 and 2004 respectivelly) and Memcached is more frature rich. Also, Memcached needs the libmemcached library and Memcache not. Of course, you can use Memcache instead of Memcached by doing little changes in the code; please see the PHP Documentation about Memcache and Memcached. Note than, in order to enable Memcached, your Webserever must have installed the PECL Memcache/Memcached extension and the correspondient libraries; ask to your Hosting administrator if they can enable them.

This Sunday I'll publish the next version of DPortal CMS, with important bugfixes and Memcached included.

Publish a comment | Return | Tags:

Comments

Return

Publish a comment

Nick: Obligatory
Email: Obligatory, not published.
URL: Optional
Your comments (max 2000 characters):
Tags <b> <i> <u> and <s> are allowed.