Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Cache_Memcached { | |
36 | const DEFAULT_HOST = 'localhost'; | |
37 | const DEFAULT_PORT = 11211; | |
38 | const DEFAULT_TIMEOUT = 3600; | |
39 | const DEFAULT_PREFIX = ''; | |
40 | const MAX_KEY_LEN = 62; | |
41 | ||
42 | /** | |
43 | * The host name of the memcached server | |
44 | * | |
45 | * @var string | |
46 | */ | |
47 | protected $_host = self::DEFAULT_HOST; | |
48 | ||
49 | /** | |
50 | * The port on which to connect on | |
51 | * | |
52 | * @var int | |
53 | */ | |
54 | protected $_port = self::DEFAULT_PORT; | |
55 | ||
56 | /** | |
57 | * The default timeout to use | |
58 | * | |
59 | * @var int | |
60 | */ | |
61 | protected $_timeout = self::DEFAULT_TIMEOUT; | |
62 | ||
63 | /** | |
64 | * The prefix prepended to cache keys. | |
65 | * | |
66 | * If we are using the same memcache instance for multiple CiviCRM | |
67 | * installs, we must have a unique prefix for each install to prevent | |
68 | * the keys from clobbering each other. | |
69 | * | |
70 | * @var string | |
71 | */ | |
72 | protected $_prefix = self::DEFAULT_PREFIX; | |
73 | ||
74 | /** | |
75 | * The actual memcache object | |
76 | * | |
77 | * @var resource | |
78 | */ | |
79 | protected $_cache; | |
80 | ||
81 | /** | |
82 | * Constructor | |
83 | * | |
dd244018 EM |
84 | * @param array $config an array of configuration params |
85 | * | |
86 | * @return \CRM_Utils_Cache_Memcached | |
6a488035 TO |
87 | */ |
88 | 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 Memcached(); | |
103 | ||
104 | if (!$this->_cache->addServer($this->_host, $this->_port)) { | |
105 | // dont use fatal here since we can go in an infinite loop | |
106 | echo 'Could not connect to Memcached server'; | |
107 | CRM_Utils_System::civiExit(); | |
108 | } | |
109 | } | |
110 | ||
5bc392e6 EM |
111 | /** |
112 | * @param $key | |
113 | * @param $value | |
114 | * | |
115 | * @return bool | |
116 | * @throws Exception | |
117 | */ | |
6a488035 TO |
118 | function set($key, &$value) { |
119 | $key = $this->cleanKey($key); | |
120 | if (!$this->_cache->set($key, $value, $this->_timeout)) { | |
121 | CRM_Core_Error::debug( 'Result Code: ', $this->_cache->getResultMessage()); | |
122 | CRM_Core_Error::fatal("memcached set failed, wondering why?, $key", $value ); | |
123 | return FALSE; | |
124 | } | |
125 | return TRUE; | |
126 | } | |
127 | ||
5bc392e6 EM |
128 | /** |
129 | * @param $key | |
130 | * | |
131 | * @return mixed | |
132 | */ | |
6a488035 TO |
133 | function &get($key) { |
134 | $key = $this->cleanKey($key); | |
135 | $result = $this->_cache->get($key); | |
136 | return $result; | |
137 | } | |
138 | ||
5bc392e6 EM |
139 | /** |
140 | * @param $key | |
141 | * | |
142 | * @return mixed | |
143 | */ | |
6a488035 TO |
144 | function delete($key) { |
145 | $key = $this->cleanKey($key); | |
146 | return $this->_cache->delete($key); | |
147 | } | |
148 | ||
5bc392e6 EM |
149 | /** |
150 | * @param $key | |
151 | * | |
152 | * @return mixed|string | |
153 | */ | |
6a488035 TO |
154 | function cleanKey($key) { |
155 | $key = preg_replace('/\s+|\W+/', '_', $this->_prefix . $key); | |
156 | if ( strlen($key) > self::MAX_KEY_LEN ) { | |
157 | $md5Key = md5($key); // this should be 32 characters in length | |
158 | $subKeyLen = self::MAX_KEY_LEN - 1 - strlen($md5Key); | |
159 | $key = substr($key, 0, $subKeyLen) . "_" . $md5Key; | |
160 | } | |
161 | return $key; | |
162 | } | |
163 | ||
e0ef6999 EM |
164 | /** |
165 | * @return mixed | |
166 | */ | |
6a488035 TO |
167 | function flush() { |
168 | return $this->_cache->flush(); | |
169 | } | |
170 | } |