Commit | Line | Data |
---|---|---|
59e56021 H |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.7 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2015 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2015 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface { | |
36 | const DEFAULT_HOST = 'localhost'; | |
37 | const DEFAULT_PORT = 11211; | |
38 | const DEFAULT_TIMEOUT = 3600; | |
39 | const DEFAULT_PREFIX = ''; | |
40 | ||
41 | /** | |
42 | * The host name of the redisd server | |
43 | * | |
44 | * @var string | |
45 | */ | |
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 | * | |
76 | * @var resource | |
77 | */ | |
78 | protected $_cache; | |
79 | ||
80 | /** | |
81 | * Constructor | |
82 | * | |
83 | * @param array $config an array of configuration params | |
84 | * | |
85 | * @return \CRM_Utils_Cache_Redis | |
86 | */ | |
87 | public function __construct($config) { | |
88 | if (isset($config['host'])) { | |
89 | $this->_host = $config['host']; | |
90 | } | |
91 | if (isset($config['port'])) { | |
92 | $this->_port = $config['port']; | |
93 | } | |
94 | if (isset($config['timeout'])) { | |
95 | $this->_timeout = $config['timeout']; | |
96 | } | |
97 | if (isset($config['prefix'])) { | |
98 | $this->_prefix = $config['prefix']; | |
99 | } | |
100 | ||
101 | $this->_cache = new Redis(); | |
102 | if (!$this->_cache->connect($this->_host, $this->_port)) { | |
103 | // dont use fatal here since we can go in an infinite loop | |
104 | echo 'Could not connect to redisd server'; | |
105 | CRM_Utils_System::civiExit(); | |
106 | } | |
107 | $this->_cache->auth(CIVICRM_DB_CACHE_PASSWORD); | |
108 | } | |
109 | ||
110 | /** | |
111 | * @param $key | |
112 | * @param $value | |
113 | * | |
114 | * @return bool | |
115 | * @throws Exception | |
116 | */ | |
117 | public function set($key, &$value) { | |
118 | if (!$this->_cache->set($this->_prefix . $key, serialize($value), $this->_timeout)) { | |
119 | CRM_Core_Error::debug('Result Code: ', $this->_cache->getResultMessage()); | |
120 | CRM_Core_Error::fatal("Redis set failed, wondering why?, $key", $value); | |
121 | return FALSE; | |
122 | } | |
123 | return TRUE; | |
124 | } | |
125 | ||
126 | /** | |
127 | * @param $key | |
128 | * | |
129 | * @return mixed | |
130 | */ | |
131 | public function get($key) { | |
132 | $result = $this->_cache->get($this->_prefix . $key); | |
133 | return unserialize($result); | |
134 | } | |
135 | ||
136 | /** | |
137 | * @param $key | |
138 | * | |
139 | * @return mixed | |
140 | */ | |
141 | public function delete($key) { | |
142 | return $this->_cache->delete($this->_prefix . $key); | |
143 | } | |
144 | ||
145 | /** | |
146 | * @return mixed | |
147 | */ | |
148 | public function flush() { | |
149 | return $this->_cache->flush(); | |
150 | } | |
151 | } |