includes/acm/acm_memcache.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acm/acm_memcache.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,138 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acm
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005, 2009 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +// Include the abstract base
    1.23 +if (!class_exists('acm_memory'))
    1.24 +{
    1.25 +	require("{$phpbb_root_path}includes/acm/acm_memory.$phpEx");
    1.26 +}
    1.27 +
    1.28 +if (!defined('PHPBB_ACM_MEMCACHE_PORT'))
    1.29 +{
    1.30 +	define('PHPBB_ACM_MEMCACHE_PORT', 11211);
    1.31 +}
    1.32 +
    1.33 +if (!defined('PHPBB_ACM_MEMCACHE_COMPRESS'))
    1.34 +{
    1.35 +	define('PHPBB_ACM_MEMCACHE_COMPRESS', false);
    1.36 +}
    1.37 +
    1.38 +if (!defined('PHPBB_ACM_MEMCACHE_HOST'))
    1.39 +{
    1.40 +	define('PHPBB_ACM_MEMCACHE_HOST', 'localhost');
    1.41 +}
    1.42 +
    1.43 +if (!defined('PHPBB_ACM_MEMCACHE'))
    1.44 +{
    1.45 +	//can define multiple servers with host1/port1,host2/port2 format
    1.46 +	define('PHPBB_ACM_MEMCACHE', PHPBB_ACM_MEMCACHE_HOST . '/' . PHPBB_ACM_MEMCACHE_PORT);
    1.47 +}
    1.48 +
    1.49 +/**
    1.50 +* ACM for Memcached
    1.51 +* @package acm
    1.52 +*/
    1.53 +class acm extends acm_memory
    1.54 +{
    1.55 +	var $extension = 'memcache';
    1.56 +
    1.57 +	var $memcache;
    1.58 +	var $flags = 0;
    1.59 +
    1.60 +	function acm()
    1.61 +	{
    1.62 +		// Call the parent constructor
    1.63 +		parent::acm_memory();
    1.64 +
    1.65 +		$this->memcache = new Memcache;
    1.66 +		foreach(explode(',', PHPBB_ACM_MEMCACHE) as $u)
    1.67 +		{
    1.68 +			$parts = explode('/', $u);
    1.69 +			$this->memcache->addServer(trim($parts[0]), trim($parts[1]));
    1.70 +		}
    1.71 +		$this->flags = (PHPBB_ACM_MEMCACHE_COMPRESS) ? MEMCACHE_COMPRESSED : 0;
    1.72 +	}
    1.73 +
    1.74 +	/**
    1.75 +	* Unload the cache resources
    1.76 +	*
    1.77 +	* @return void
    1.78 +	*/
    1.79 +	function unload()
    1.80 +	{
    1.81 +		parent::unload();
    1.82 +
    1.83 +		$this->memcache->close();
    1.84 +	}
    1.85 +
    1.86 +	/**
    1.87 +	* Purge cache data
    1.88 +	*
    1.89 +	* @return void
    1.90 +	*/
    1.91 +	function purge()
    1.92 +	{
    1.93 +		$this->memcache->flush();
    1.94 +
    1.95 +		parent::purge();
    1.96 +	}
    1.97 +
    1.98 +	/**
    1.99 +	* Fetch an item from the cache
   1.100 +	*
   1.101 +	* @access protected
   1.102 +	* @param string $var Cache key
   1.103 +	* @return mixed Cached data
   1.104 +	*/
   1.105 +	function _read($var)
   1.106 +	{
   1.107 +		return $this->memcache->get($this->key_prefix . $var);
   1.108 +	}
   1.109 +
   1.110 +	/**
   1.111 +	* Store data in the cache
   1.112 +	*
   1.113 +	* @access protected
   1.114 +	* @param string $var Cache key
   1.115 +	* @param mixed $data Data to store
   1.116 +	* @param int $ttl Time-to-live of cached data
   1.117 +	* @return bool True if the operation succeeded
   1.118 +	*/
   1.119 +	function _write($var, $data, $ttl = 2592000)
   1.120 +	{
   1.121 +		if (!$this->memcache->replace($this->key_prefix . $var, $data, $this->flags, $ttl))
   1.122 +		{
   1.123 +			return $this->memcache->set($this->key_prefix . $var, $data, $this->flags, $ttl);
   1.124 +		}
   1.125 +		return true;
   1.126 +	}
   1.127 +
   1.128 +	/**
   1.129 +	* Remove an item from the cache
   1.130 +	*
   1.131 +	* @access protected
   1.132 +	* @param string $var Cache key
   1.133 +	* @return bool True if the operation succeeded
   1.134 +	*/
   1.135 +	function _delete($var)
   1.136 +	{
   1.137 +		return $this->memcache->delete($this->key_prefix . $var);
   1.138 +	}
   1.139 +}
   1.140 +
   1.141 +?>