includes/acm/acm_redis.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acm/acm_redis.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,145 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acm
     1.8 +* @copyright (c) 2011 phpBB Group
     1.9 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.10 +*
    1.11 +*/
    1.12 +
    1.13 +/**
    1.14 +* @ignore
    1.15 +*/
    1.16 +if (!defined('IN_PHPBB'))
    1.17 +{
    1.18 +	exit;
    1.19 +}
    1.20 +
    1.21 +// Include the abstract base
    1.22 +if (!class_exists('acm_memory'))
    1.23 +{
    1.24 +	require("{$phpbb_root_path}includes/acm/acm_memory.$phpEx");
    1.25 +}
    1.26 +
    1.27 +if (!defined('PHPBB_ACM_REDIS_PORT'))
    1.28 +{
    1.29 +	define('PHPBB_ACM_REDIS_PORT', 6379);
    1.30 +}
    1.31 +
    1.32 +if (!defined('PHPBB_ACM_REDIS_HOST'))
    1.33 +{
    1.34 +	define('PHPBB_ACM_REDIS_HOST', 'localhost');
    1.35 +}
    1.36 +
    1.37 +/**
    1.38 +* ACM for Redis
    1.39 +*
    1.40 +* Compatible with the php extension phpredis available
    1.41 +* at https://github.com/nicolasff/phpredis
    1.42 +*
    1.43 +* @package acm
    1.44 +*/
    1.45 +class acm extends acm_memory
    1.46 +{
    1.47 +	var $extension = 'redis';
    1.48 +
    1.49 +	var $redis;
    1.50 +
    1.51 +	function acm()
    1.52 +	{
    1.53 +		// Call the parent constructor
    1.54 +		parent::acm_memory();
    1.55 +
    1.56 +		$this->redis = new Redis();
    1.57 +		$this->redis->connect(PHPBB_ACM_REDIS_HOST, PHPBB_ACM_REDIS_PORT);
    1.58 +
    1.59 +		if (defined('PHPBB_ACM_REDIS_PASSWORD'))
    1.60 +		{
    1.61 +			if (!$this->redis->auth(PHPBB_ACM_REDIS_PASSWORD))
    1.62 +			{
    1.63 +				global $acm_type;
    1.64 +
    1.65 +				trigger_error("Incorrect password for the ACM module $acm_type.", E_USER_ERROR);
    1.66 +			}
    1.67 +		}
    1.68 +
    1.69 +		$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
    1.70 +		$this->redis->setOption(Redis::OPT_PREFIX, $this->key_prefix);
    1.71 +
    1.72 +		if (defined('PHPBB_ACM_REDIS_DB'))
    1.73 +		{
    1.74 +			if (!$this->redis->select(PHPBB_ACM_REDIS_DB))
    1.75 +			{
    1.76 +				global $acm_type;
    1.77 +
    1.78 +				trigger_error("Incorrect database for the ACM module $acm_type.", E_USER_ERROR);
    1.79 +			}
    1.80 +		}
    1.81 +	}
    1.82 +
    1.83 +	/**
    1.84 +	* Unload the cache resources
    1.85 +	*
    1.86 +	* @return void
    1.87 +	*/
    1.88 +	function unload()
    1.89 +	{
    1.90 +		parent::unload();
    1.91 +
    1.92 +		$this->redis->close();
    1.93 +	}
    1.94 +
    1.95 +	/**
    1.96 +	* Purge cache data
    1.97 +	*
    1.98 +	* @return void
    1.99 +	*/
   1.100 +	function purge()
   1.101 +	{
   1.102 +		$this->redis->flushDB();
   1.103 +
   1.104 +		parent::purge();
   1.105 +	}
   1.106 +
   1.107 +	/**
   1.108 +	* Fetch an item from the cache
   1.109 +	*
   1.110 +	* @access protected
   1.111 +	* @param string $var Cache key
   1.112 +	* @return mixed Cached data
   1.113 +	*/
   1.114 +	function _read($var)
   1.115 +	{
   1.116 +		return $this->redis->get($var);
   1.117 +	}
   1.118 +
   1.119 +	/**
   1.120 +	* Store data in the cache
   1.121 +	*
   1.122 +	* @access protected
   1.123 +	* @param string $var Cache key
   1.124 +	* @param mixed $data Data to store
   1.125 +	* @param int $ttl Time-to-live of cached data
   1.126 +	* @return bool True if the operation succeeded
   1.127 +	*/
   1.128 +	function _write($var, $data, $ttl = 2592000)
   1.129 +	{
   1.130 +		return $this->redis->setex($var, $ttl, $data);
   1.131 +	}
   1.132 +
   1.133 +	/**
   1.134 +	* Remove an item from the cache
   1.135 +	*
   1.136 +	* @access protected
   1.137 +	* @param string $var Cache key
   1.138 +	* @return bool True if the operation succeeded
   1.139 +	*/
   1.140 +	function _delete($var)
   1.141 +	{
   1.142 +		if ($this->redis->delete($var) > 0)
   1.143 +		{
   1.144 +			return true;
   1.145 +		}
   1.146 +		return false;
   1.147 +	}
   1.148 +}