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 { | |
36 | const DEFAULT_HOST = 'localhost'; | |
9d2f1acb | 37 | const DEFAULT_PORT = 6379; |
59e56021 H |
38 | const DEFAULT_TIMEOUT = 3600; |
39 | const DEFAULT_PREFIX = ''; | |
40 | ||
41 | /** | |
42 | * The host name of the redisd server | |
43 | * | |
44 | * @var string | |
4ca97b71 | 45 | */ |
59e56021 H |
46 | protected $_host = self::DEFAULT_HOST; |
47 | ||
48 | /** | |
49 | * The port on which to connect on | |
50 | * | |
51 | * @var int | |
52 | */ | |
53 | protected $_port = self::DEFAULT_PORT; | |
54 | ||
55 | /** | |
56 | * The default timeout to use | |
57 | * | |
58 | * @var int | |
59 | */ | |
60 | protected $_timeout = self::DEFAULT_TIMEOUT; | |
61 | ||
62 | /** | |
63 | * The prefix prepended to cache keys. | |
64 | * | |
65 | * If we are using the same redis instance for multiple CiviCRM | |
66 | * installs, we must have a unique prefix for each install to prevent | |
67 | * the keys from clobbering each other. | |
68 | * | |
69 | * @var string | |
70 | */ | |
71 | protected $_prefix = self::DEFAULT_PREFIX; | |
72 | ||
73 | /** | |
74 | * The actual redis object | |
75 | * | |
6db6aac0 | 76 | * @var Redis |
59e56021 H |
77 | */ |
78 | protected $_cache; | |
79 | ||
80 | /** | |
81 | * Constructor | |
82 | * | |
4ca97b71 H |
83 | * @param array $config |
84 | * An array of configuration params. | |
59e56021 H |
85 | * |
86 | * @return \CRM_Utils_Cache_Redis | |
87 | */ | |
88 | public function __construct($config) { | |
89 | if (isset($config['host'])) { | |
90 | $this->_host = $config['host']; | |
91 | } | |
92 | if (isset($config['port'])) { | |
93 | $this->_port = $config['port']; | |
94 | } | |
95 | if (isset($config['timeout'])) { | |
96 | $this->_timeout = $config['timeout']; | |
97 | } | |
98 | if (isset($config['prefix'])) { | |
99 | $this->_prefix = $config['prefix']; | |
100 | } | |
101 | ||
102 | $this->_cache = new Redis(); | |
103 | if (!$this->_cache->connect($this->_host, $this->_port)) { | |
104 | // dont use fatal here since we can go in an infinite loop | |
105 | echo 'Could not connect to redisd server'; | |
106 | CRM_Utils_System::civiExit(); | |
107 | } | |
62da9ed4 TO |
108 | if (CRM_Utils_Constant::value('CIVICRM_DB_CACHE_PASSWORD')) { |
109 | $this->_cache->auth(CIVICRM_DB_CACHE_PASSWORD); | |
110 | } | |
59e56021 H |
111 | } |
112 | ||
113 | /** | |
114 | * @param $key | |
115 | * @param $value | |
858451a9 | 116 | * @param null|int|\DateInterval $ttl |
59e56021 H |
117 | * |
118 | * @return bool | |
119 | * @throws Exception | |
120 | */ | |
858451a9 TO |
121 | public function set($key, $value, $ttl = NULL) { |
122 | if ($ttl !== NULL) { | |
123 | throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL"); | |
124 | } | |
59e56021 | 125 | if (!$this->_cache->set($this->_prefix . $key, serialize($value), $this->_timeout)) { |
aed6cc22 TO |
126 | if (PHP_SAPI === 'cli' || (Civi\Core\Container::isContainerBooted() && CRM_Core_Permission::check('view debug output'))) { |
127 | CRM_Core_Error::fatal("Redis set ($key) failed: " . $this->_cache->getLastError()); | |
128 | } | |
129 | else { | |
130 | Civi::log()->error("Redis set ($key) failed: " . $this->_cache->getLastError()); | |
131 | CRM_Core_Error::fatal("Redis set ($key) failed"); | |
132 | } | |
59e56021 H |
133 | return FALSE; |
134 | } | |
135 | return TRUE; | |
136 | } | |
137 | ||
138 | /** | |
139 | * @param $key | |
2da67cc5 | 140 | * @param mixed $default |
59e56021 H |
141 | * |
142 | * @return mixed | |
143 | */ | |
2da67cc5 | 144 | public function get($key, $default = NULL) { |
59e56021 | 145 | $result = $this->_cache->get($this->_prefix . $key); |
2da67cc5 | 146 | return ($result === FALSE) ? $default : unserialize($result); |
59e56021 H |
147 | } |
148 | ||
149 | /** | |
150 | * @param $key | |
151 | * | |
152 | * @return mixed | |
153 | */ | |
154 | public function delete($key) { | |
155 | return $this->_cache->delete($this->_prefix . $key); | |
156 | } | |
157 | ||
158 | /** | |
159 | * @return mixed | |
160 | */ | |
161 | public function flush() { | |
fbebc7a0 TO |
162 | // FIXME: Ideally, we'd map each prefix to a different 'hash' object in Redis, |
163 | // and this would be simpler. However, that needs to go in tandem with a | |
164 | // more general rethink of cache expiration/TTL. | |
165 | ||
166 | $keys = $this->_cache->keys($this->_prefix . '*'); | |
167 | return $this->_cache->del($keys); | |
59e56021 | 168 | } |
4ca97b71 | 169 | |
59e56021 | 170 | } |