includes/acm/acm_memcache.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:11:31 +0400
changeset 14 88d35b740f20
parent 0 ceef6d64c21d
permissions -rw-r--r--
HG ignore
     1 <?php
     2 /**
     3 *
     4 * @package acm
     5 * @version $Id$
     6 * @copyright (c) 2005, 2009 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 /**
    12 * @ignore
    13 */
    14 if (!defined('IN_PHPBB'))
    15 {
    16 	exit;
    17 }
    18 
    19 // Include the abstract base
    20 if (!class_exists('acm_memory'))
    21 {
    22 	require("{$phpbb_root_path}includes/acm/acm_memory.$phpEx");
    23 }
    24 
    25 if (!defined('PHPBB_ACM_MEMCACHE_PORT'))
    26 {
    27 	define('PHPBB_ACM_MEMCACHE_PORT', 11211);
    28 }
    29 
    30 if (!defined('PHPBB_ACM_MEMCACHE_COMPRESS'))
    31 {
    32 	define('PHPBB_ACM_MEMCACHE_COMPRESS', false);
    33 }
    34 
    35 if (!defined('PHPBB_ACM_MEMCACHE_HOST'))
    36 {
    37 	define('PHPBB_ACM_MEMCACHE_HOST', 'localhost');
    38 }
    39 
    40 if (!defined('PHPBB_ACM_MEMCACHE'))
    41 {
    42 	//can define multiple servers with host1/port1,host2/port2 format
    43 	define('PHPBB_ACM_MEMCACHE', PHPBB_ACM_MEMCACHE_HOST . '/' . PHPBB_ACM_MEMCACHE_PORT);
    44 }
    45 
    46 /**
    47 * ACM for Memcached
    48 * @package acm
    49 */
    50 class acm extends acm_memory
    51 {
    52 	var $extension = 'memcache';
    53 
    54 	var $memcache;
    55 	var $flags = 0;
    56 
    57 	function acm()
    58 	{
    59 		// Call the parent constructor
    60 		parent::acm_memory();
    61 
    62 		$this->memcache = new Memcache;
    63 		foreach(explode(',', PHPBB_ACM_MEMCACHE) as $u)
    64 		{
    65 			$parts = explode('/', $u);
    66 			$this->memcache->addServer(trim($parts[0]), trim($parts[1]));
    67 		}
    68 		$this->flags = (PHPBB_ACM_MEMCACHE_COMPRESS) ? MEMCACHE_COMPRESSED : 0;
    69 	}
    70 
    71 	/**
    72 	* Unload the cache resources
    73 	*
    74 	* @return void
    75 	*/
    76 	function unload()
    77 	{
    78 		parent::unload();
    79 
    80 		$this->memcache->close();
    81 	}
    82 
    83 	/**
    84 	* Purge cache data
    85 	*
    86 	* @return void
    87 	*/
    88 	function purge()
    89 	{
    90 		$this->memcache->flush();
    91 
    92 		parent::purge();
    93 	}
    94 
    95 	/**
    96 	* Fetch an item from the cache
    97 	*
    98 	* @access protected
    99 	* @param string $var Cache key
   100 	* @return mixed Cached data
   101 	*/
   102 	function _read($var)
   103 	{
   104 		return $this->memcache->get($this->key_prefix . $var);
   105 	}
   106 
   107 	/**
   108 	* Store data in the cache
   109 	*
   110 	* @access protected
   111 	* @param string $var Cache key
   112 	* @param mixed $data Data to store
   113 	* @param int $ttl Time-to-live of cached data
   114 	* @return bool True if the operation succeeded
   115 	*/
   116 	function _write($var, $data, $ttl = 2592000)
   117 	{
   118 		if (!$this->memcache->replace($this->key_prefix . $var, $data, $this->flags, $ttl))
   119 		{
   120 			return $this->memcache->set($this->key_prefix . $var, $data, $this->flags, $ttl);
   121 		}
   122 		return true;
   123 	}
   124 
   125 	/**
   126 	* Remove an item from the cache
   127 	*
   128 	* @access protected
   129 	* @param string $var Cache key
   130 	* @return bool True if the operation succeeded
   131 	*/
   132 	function _delete($var)
   133 	{
   134 		return $this->memcache->delete($this->key_prefix . $var);
   135 	}
   136 }
   137 
   138 ?>