includes/acm/acm_redis.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 02:39:20 +0400
changeset 18 36b05a593b41
parent 0 ceef6d64c21d
permissions -rw-r--r--
Fix username allowed characters (like Cubometry game)
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package acm
indvdum@0
     5
* @copyright (c) 2011 phpBB Group
indvdum@0
     6
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     7
*
indvdum@0
     8
*/
indvdum@0
     9
indvdum@0
    10
/**
indvdum@0
    11
* @ignore
indvdum@0
    12
*/
indvdum@0
    13
if (!defined('IN_PHPBB'))
indvdum@0
    14
{
indvdum@0
    15
	exit;
indvdum@0
    16
}
indvdum@0
    17
indvdum@0
    18
// Include the abstract base
indvdum@0
    19
if (!class_exists('acm_memory'))
indvdum@0
    20
{
indvdum@0
    21
	require("{$phpbb_root_path}includes/acm/acm_memory.$phpEx");
indvdum@0
    22
}
indvdum@0
    23
indvdum@0
    24
if (!defined('PHPBB_ACM_REDIS_PORT'))
indvdum@0
    25
{
indvdum@0
    26
	define('PHPBB_ACM_REDIS_PORT', 6379);
indvdum@0
    27
}
indvdum@0
    28
indvdum@0
    29
if (!defined('PHPBB_ACM_REDIS_HOST'))
indvdum@0
    30
{
indvdum@0
    31
	define('PHPBB_ACM_REDIS_HOST', 'localhost');
indvdum@0
    32
}
indvdum@0
    33
indvdum@0
    34
/**
indvdum@0
    35
* ACM for Redis
indvdum@0
    36
*
indvdum@0
    37
* Compatible with the php extension phpredis available
indvdum@0
    38
* at https://github.com/nicolasff/phpredis
indvdum@0
    39
*
indvdum@0
    40
* @package acm
indvdum@0
    41
*/
indvdum@0
    42
class acm extends acm_memory
indvdum@0
    43
{
indvdum@0
    44
	var $extension = 'redis';
indvdum@0
    45
indvdum@0
    46
	var $redis;
indvdum@0
    47
indvdum@0
    48
	function acm()
indvdum@0
    49
	{
indvdum@0
    50
		// Call the parent constructor
indvdum@0
    51
		parent::acm_memory();
indvdum@0
    52
indvdum@0
    53
		$this->redis = new Redis();
indvdum@0
    54
		$this->redis->connect(PHPBB_ACM_REDIS_HOST, PHPBB_ACM_REDIS_PORT);
indvdum@0
    55
indvdum@0
    56
		if (defined('PHPBB_ACM_REDIS_PASSWORD'))
indvdum@0
    57
		{
indvdum@0
    58
			if (!$this->redis->auth(PHPBB_ACM_REDIS_PASSWORD))
indvdum@0
    59
			{
indvdum@0
    60
				global $acm_type;
indvdum@0
    61
indvdum@0
    62
				trigger_error("Incorrect password for the ACM module $acm_type.", E_USER_ERROR);
indvdum@0
    63
			}
indvdum@0
    64
		}
indvdum@0
    65
indvdum@0
    66
		$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
indvdum@0
    67
		$this->redis->setOption(Redis::OPT_PREFIX, $this->key_prefix);
indvdum@0
    68
indvdum@0
    69
		if (defined('PHPBB_ACM_REDIS_DB'))
indvdum@0
    70
		{
indvdum@0
    71
			if (!$this->redis->select(PHPBB_ACM_REDIS_DB))
indvdum@0
    72
			{
indvdum@0
    73
				global $acm_type;
indvdum@0
    74
indvdum@0
    75
				trigger_error("Incorrect database for the ACM module $acm_type.", E_USER_ERROR);
indvdum@0
    76
			}
indvdum@0
    77
		}
indvdum@0
    78
	}
indvdum@0
    79
indvdum@0
    80
	/**
indvdum@0
    81
	* Unload the cache resources
indvdum@0
    82
	*
indvdum@0
    83
	* @return void
indvdum@0
    84
	*/
indvdum@0
    85
	function unload()
indvdum@0
    86
	{
indvdum@0
    87
		parent::unload();
indvdum@0
    88
indvdum@0
    89
		$this->redis->close();
indvdum@0
    90
	}
indvdum@0
    91
indvdum@0
    92
	/**
indvdum@0
    93
	* Purge cache data
indvdum@0
    94
	*
indvdum@0
    95
	* @return void
indvdum@0
    96
	*/
indvdum@0
    97
	function purge()
indvdum@0
    98
	{
indvdum@0
    99
		$this->redis->flushDB();
indvdum@0
   100
indvdum@0
   101
		parent::purge();
indvdum@0
   102
	}
indvdum@0
   103
indvdum@0
   104
	/**
indvdum@0
   105
	* Fetch an item from the cache
indvdum@0
   106
	*
indvdum@0
   107
	* @access protected
indvdum@0
   108
	* @param string $var Cache key
indvdum@0
   109
	* @return mixed Cached data
indvdum@0
   110
	*/
indvdum@0
   111
	function _read($var)
indvdum@0
   112
	{
indvdum@0
   113
		return $this->redis->get($var);
indvdum@0
   114
	}
indvdum@0
   115
indvdum@0
   116
	/**
indvdum@0
   117
	* Store data in the cache
indvdum@0
   118
	*
indvdum@0
   119
	* @access protected
indvdum@0
   120
	* @param string $var Cache key
indvdum@0
   121
	* @param mixed $data Data to store
indvdum@0
   122
	* @param int $ttl Time-to-live of cached data
indvdum@0
   123
	* @return bool True if the operation succeeded
indvdum@0
   124
	*/
indvdum@0
   125
	function _write($var, $data, $ttl = 2592000)
indvdum@0
   126
	{
indvdum@0
   127
		return $this->redis->setex($var, $ttl, $data);
indvdum@0
   128
	}
indvdum@0
   129
indvdum@0
   130
	/**
indvdum@0
   131
	* Remove an item from the cache
indvdum@0
   132
	*
indvdum@0
   133
	* @access protected
indvdum@0
   134
	* @param string $var Cache key
indvdum@0
   135
	* @return bool True if the operation succeeded
indvdum@0
   136
	*/
indvdum@0
   137
	function _delete($var)
indvdum@0
   138
	{
indvdum@0
   139
		if ($this->redis->delete($var) > 0)
indvdum@0
   140
		{
indvdum@0
   141
			return true;
indvdum@0
   142
		}
indvdum@0
   143
		return false;
indvdum@0
   144
	}
indvdum@0
   145
}