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