Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/cache/storage/memcache.php

Documentation is available at memcache.php

  1. <?php
  2. /**
  3.  * @version        $Id: memcache.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Cache
  6.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * Memcache cache storage handler
  20.  *
  21.  * @author        Louis Landry <louis.landry@joomla.org>
  22.  * @author        Mitch Pirtle
  23.  * @package        Joomla.Framework
  24.  * @subpackage    Cache
  25.  * @since        1.5
  26.  */
  27. {
  28.     /**
  29.      * Resource for the current memcached connection.
  30.      * @var resource 
  31.      */
  32.     var $_db;
  33.  
  34.     /**
  35.      * Use compression?
  36.      * @var int 
  37.      */
  38.     var $_compress = null;
  39.  
  40.     /**
  41.      * Use persistent connections
  42.      * @var boolean 
  43.      */
  44.     var $_persistent = false;
  45.  
  46.     /**
  47.      * Constructor
  48.      *
  49.      * @access protected
  50.      * @param array $options optional parameters
  51.      */
  52.     function __construct$options array() )
  53.     {
  54.         if (!$this->test()) {
  55.             return JError::raiseError(404"The memcache extension is not available");
  56.         }
  57.  
  58.         parent::__construct($options);
  59.  
  60.         $config =JFactory::getConfig();
  61.  
  62.         $params $config->getValue('config.memcache_settings');
  63.         if (!is_array($params)) {
  64.             $params unserialize(stripslashes($params));
  65.         }
  66.  
  67.         if (!$params{
  68.             $params array();
  69.         }
  70.  
  71.         $this->_compress    = (isset($params['compression'])) $params['compression'0;
  72.         $this->_persistent    = (isset($params['persistent'])) $params['persistent'false;
  73.  
  74.         // This will be an array of loveliness
  75.         $this->_servers    (isset($params['servers'])) $params['servers'array();
  76.  
  77.         // Create the memcache connection
  78.         $this->_db = new Memcache;
  79.         for ($i=0$n=count($this->_servers)$i $n$i++)
  80.         {
  81.             $server $this->_servers[$i];
  82.             $this->_db->addServer($server['host']$server['port']$this->_persistent);
  83.         }
  84.  
  85.         // Get the site hash
  86.         $this->_hash $config->getValue('config.secret');
  87.     }
  88.  
  89.     /**
  90.      * Get cached data from memcache by id and group
  91.      *
  92.      * @access    public
  93.      * @param    string    $id            The cache data id
  94.      * @param    string    $group        The cache data group
  95.      * @param    boolean    $checkTime    True to verify cache time expiration threshold
  96.      * @return    mixed    Boolean false on failure or a cached data string
  97.      * @since    1.5
  98.      */
  99.     function get($id$group$checkTime)
  100.     {
  101.         $cache_id $this->_getCacheId($id$group);
  102.         $this->_setExpire($cache_id);
  103.         return $this->_db->get($cache_id);
  104.     }
  105.  
  106.     /**
  107.      * Store the data to memcache by id and group
  108.      *
  109.      * @access    public
  110.      * @param    string    $id        The cache data id
  111.      * @param    string    $group    The cache data group
  112.      * @param    string    $data    The data to store in cache
  113.      * @return    boolean    True on success, false otherwise
  114.      * @since    1.5
  115.      */
  116.     function store($id$group$data)
  117.     {
  118.         $cache_id $this->_getCacheId($id$group);
  119.         $this->_db->set($cache_id.'_expire'time()00);
  120.         return $this->_db->set($cache_id$data$this->_compress0);
  121.     }
  122.  
  123.     /**
  124.      * Remove a cached data entry by id and group
  125.      *
  126.      * @access    public
  127.      * @param    string    $id        The cache data id
  128.      * @param    string    $group    The cache data group
  129.      * @return    boolean    True on success, false otherwise
  130.      * @since    1.5
  131.      */
  132.     function remove($id$group)
  133.     {
  134.         $cache_id $this->_getCacheId($id$group);
  135.         $this->_db->delete($cache_id.'_expire');
  136.         return $this->_db->delete($cache_id);
  137.     }
  138.  
  139.     /**
  140.      * Clean cache for a group given a mode.
  141.      *
  142.      * group mode        : cleans all cache in the group
  143.      * notgroup mode    : cleans all cache not in the group
  144.      *
  145.      * @access    public
  146.      * @param    string    $group    The cache data group
  147.      * @param    string    $mode    The mode for cleaning cache [group|notgroup]
  148.      * @return    boolean    True on success, false otherwise
  149.      * @since    1.5
  150.      */
  151.     function clean($group$mode)
  152.     {
  153.         return true;
  154.     }
  155.  
  156.     /**
  157.      * Garbage collect expired cache data
  158.      *
  159.      * @access public
  160.      * @return boolean  True on success, false otherwise.
  161.      */
  162.     function gc()
  163.     {
  164.         return true;
  165.     }
  166.  
  167.     /**
  168.      * Test to see if the cache storage is available.
  169.      *
  170.      * @static
  171.      * @access public
  172.      * @return boolean  True on success, false otherwise.
  173.      */
  174.     function test()
  175.     {
  176.         return (extension_loaded('memcache'&& class_exists('Memcache'));
  177.     }
  178.  
  179.     /**
  180.      * Set expire time on each call since memcache sets it on cache creation.
  181.      *
  182.      * @access private
  183.      *
  184.      * @param string  $key   Cache key to expire.
  185.      * @param integer $lifetime  Lifetime of the data in seconds.
  186.      */
  187.     function _setExpire($key)
  188.     {
  189.         $lifetime    $this->_lifetime;
  190.         $expire        $this->_db->get($key.'_expire');
  191.  
  192.         // set prune period
  193.         if ($expire $lifetime time()) {
  194.             $this->_db->delete($key);
  195.             $this->_db->delete($key.'_expire');
  196.         else {
  197.             $this->_db->replace($key.'_expire'time());
  198.         }
  199.     }
  200.  
  201.     /**
  202.      * Get a cache_id string from an id/group pair
  203.      *
  204.      * @access    private
  205.      * @param    string    $id        The cache data id
  206.      * @param    string    $group    The cache data group
  207.      * @return    string    The cache_id string
  208.      * @since    1.5
  209.      */
  210.     function _getCacheId($id$group)
  211.     {
  212.         $name    md5($this->_application.'-'.$id.'-'.$this->_hash.'-'.$this->_language);
  213.         return 'cache_'.$group.'-'.$name;
  214.     }
  215. }

Documentation generated on Tue, 29 Jan 2008 18:48:18 +0000 by phpDocumentor 1.3.1