Commit | Line | Data |
---|---|---|
59e56021 H |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
59e56021 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
59e56021 H |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
4ca97b71 | 26 | */ |
59e56021 H |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
59e56021 H |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface { | |
0d64c8fa TO |
36 | |
37 | use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. | |
9f70b0e4 | 38 | use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation |
0d64c8fa | 39 | |
59e56021 | 40 | const DEFAULT_HOST = 'localhost'; |
9d2f1acb | 41 | const DEFAULT_PORT = 6379; |
59e56021 H |
42 | const DEFAULT_TIMEOUT = 3600; |
43 | const DEFAULT_PREFIX = ''; | |
44 | ||
45 | /** | |
46 | * The host name of the redisd server | |
47 | * | |
48 | * @var string | |
4ca97b71 | 49 | */ |
59e56021 H |
50 | protected $_host = self::DEFAULT_HOST; |
51 | ||
52 | /** | |
53 | * The port on which to connect on | |
54 | * | |
55 | * @var int | |
56 | */ | |
57 | protected $_port = self::DEFAULT_PORT; | |
58 | ||
59 | /** | |
60 | * The default timeout to use | |
61 | * | |
62 | * @var int | |
63 | */ | |
64 | protected $_timeout = self::DEFAULT_TIMEOUT; | |
65 | ||
66 | /** | |
67 | * The prefix prepended to cache keys. | |
68 | * | |
69 | * If we are using the same redis instance for multiple CiviCRM | |
70 | * installs, we must have a unique prefix for each install to prevent | |
71 | * the keys from clobbering each other. | |
72 | * | |
73 | * @var string | |
74 | */ | |
75 | protected $_prefix = self::DEFAULT_PREFIX; | |
76 | ||
77 | /** | |
78 | * The actual redis object | |
79 | * | |
6db6aac0 | 80 | * @var Redis |
59e56021 H |
81 | */ |
82 | protected $_cache; | |
83 | ||
84 | /** | |
85 | * Constructor | |
86 | * | |
4ca97b71 H |
87 | * @param array $config |
88 | * An array of configuration params. | |
59e56021 H |
89 | * |
90 | * @return \CRM_Utils_Cache_Redis | |
91 | */ | |
92 | public function __construct($config) { | |
93 | if (isset($config['host'])) { | |
94 | $this->_host = $config['host']; | |
95 | } | |
96 | if (isset($config['port'])) { | |
97 | $this->_port = $config['port']; | |
98 | } | |
99 | if (isset($config['timeout'])) { | |
100 | $this->_timeout = $config['timeout']; | |
101 | } | |
102 | if (isset($config['prefix'])) { | |
103 | $this->_prefix = $config['prefix']; | |
104 | } | |
105 | ||
106 | $this->_cache = new Redis(); | |
107 | if (!$this->_cache->connect($this->_host, $this->_port)) { | |
108 | // dont use fatal here since we can go in an infinite loop | |
109 | echo 'Could not connect to redisd server'; | |
110 | CRM_Utils_System::civiExit(); | |
111 | } | |
62da9ed4 TO |
112 | if (CRM_Utils_Constant::value('CIVICRM_DB_CACHE_PASSWORD')) { |
113 | $this->_cache->auth(CIVICRM_DB_CACHE_PASSWORD); | |
114 | } | |
59e56021 H |
115 | } |
116 | ||
117 | /** | |
118 | * @param $key | |
119 | * @param $value | |
858451a9 | 120 | * @param null|int|\DateInterval $ttl |
59e56021 H |
121 | * |
122 | * @return bool | |
123 | * @throws Exception | |
124 | */ | |
858451a9 | 125 | public function set($key, $value, $ttl = NULL) { |
26733eb5 TO |
126 | CRM_Utils_Cache::assertValidKey($key); |
127 | if (is_int($ttl) && $ttl <= 0) { | |
128 | return $this->delete($key); | |
858451a9 | 129 | } |
26733eb5 TO |
130 | $ttl = CRM_Utils_Date::convertCacheTtl($ttl, self::DEFAULT_TIMEOUT); |
131 | if (!$this->_cache->setex($this->_prefix . $key, $ttl, serialize($value))) { | |
aed6cc22 | 132 | if (PHP_SAPI === 'cli' || (Civi\Core\Container::isContainerBooted() && CRM_Core_Permission::check('view debug output'))) { |
26733eb5 | 133 | throw new CRM_Utils_Cache_CacheException("Redis set ($key) failed: " . $this->_cache->getLastError()); |
aed6cc22 TO |
134 | } |
135 | else { | |
136 | Civi::log()->error("Redis set ($key) failed: " . $this->_cache->getLastError()); | |
26733eb5 | 137 | throw new CRM_Utils_Cache_CacheException("Redis set ($key) failed"); |
aed6cc22 | 138 | } |
59e56021 H |
139 | return FALSE; |
140 | } | |
141 | return TRUE; | |
142 | } | |
143 | ||
144 | /** | |
145 | * @param $key | |
2da67cc5 | 146 | * @param mixed $default |
59e56021 H |
147 | * |
148 | * @return mixed | |
149 | */ | |
2da67cc5 | 150 | public function get($key, $default = NULL) { |
26733eb5 | 151 | CRM_Utils_Cache::assertValidKey($key); |
59e56021 | 152 | $result = $this->_cache->get($this->_prefix . $key); |
2da67cc5 | 153 | return ($result === FALSE) ? $default : unserialize($result); |
59e56021 H |
154 | } |
155 | ||
156 | /** | |
157 | * @param $key | |
158 | * | |
eec321a4 | 159 | * @return bool |
59e56021 H |
160 | */ |
161 | public function delete($key) { | |
26733eb5 | 162 | CRM_Utils_Cache::assertValidKey($key); |
eec321a4 TO |
163 | $this->_cache->delete($this->_prefix . $key); |
164 | return TRUE; | |
59e56021 H |
165 | } |
166 | ||
167 | /** | |
124e5288 | 168 | * @return bool |
59e56021 H |
169 | */ |
170 | public function flush() { | |
fbebc7a0 TO |
171 | // FIXME: Ideally, we'd map each prefix to a different 'hash' object in Redis, |
172 | // and this would be simpler. However, that needs to go in tandem with a | |
173 | // more general rethink of cache expiration/TTL. | |
174 | ||
175 | $keys = $this->_cache->keys($this->_prefix . '*'); | |
124e5288 TO |
176 | $this->_cache->del($keys); |
177 | return TRUE; | |
59e56021 | 178 | } |
4ca97b71 | 179 | |
c31de879 TO |
180 | public function clear() { |
181 | return $this->flush(); | |
182 | } | |
183 | ||
59e56021 | 184 | } |