Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 | 32 | */ |
9f49773e | 33 | class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface { |
353ffa53 TO |
34 | const DEFAULT_HOST = 'localhost'; |
35 | const DEFAULT_PORT = 11211; | |
6a488035 | 36 | const DEFAULT_TIMEOUT = 3600; |
353ffa53 | 37 | const DEFAULT_PREFIX = ''; |
6a488035 TO |
38 | |
39 | /** | |
fe482240 | 40 | * The host name of the memcached server. |
6a488035 TO |
41 | * |
42 | * @var string | |
43 | */ | |
44 | protected $_host = self::DEFAULT_HOST; | |
45 | ||
46 | /** | |
fe482240 | 47 | * The port on which to connect on. |
6a488035 TO |
48 | * |
49 | * @var int | |
50 | */ | |
51 | protected $_port = self::DEFAULT_PORT; | |
52 | ||
53 | /** | |
fe482240 | 54 | * The default timeout to use. |
6a488035 TO |
55 | * |
56 | * @var int | |
57 | */ | |
58 | protected $_timeout = self::DEFAULT_TIMEOUT; | |
59 | ||
60 | /** | |
61 | * The prefix prepended to cache keys. | |
62 | * | |
63 | * If we are using the same memcache instance for multiple CiviCRM | |
64 | * installs, we must have a unique prefix for each install to prevent | |
65 | * the keys from clobbering each other. | |
66 | * | |
67 | * @var string | |
68 | */ | |
69 | protected $_prefix = self::DEFAULT_PREFIX; | |
70 | ||
71 | /** | |
fe482240 | 72 | * The actual memcache object. |
6a488035 | 73 | * |
41e0c250 | 74 | * @var Memcache |
6a488035 TO |
75 | */ |
76 | protected $_cache; | |
77 | ||
78 | /** | |
fe482240 | 79 | * Constructor. |
6a488035 | 80 | * |
77855840 TO |
81 | * @param array $config |
82 | * An array of configuration params. | |
6a488035 | 83 | * |
77b97be7 | 84 | * @return \CRM_Utils_Cache_Memcache |
6a488035 | 85 | */ |
00be9182 | 86 | public function __construct($config) { |
6a488035 TO |
87 | if (isset($config['host'])) { |
88 | $this->_host = $config['host']; | |
89 | } | |
90 | if (isset($config['port'])) { | |
91 | $this->_port = $config['port']; | |
92 | } | |
93 | if (isset($config['timeout'])) { | |
94 | $this->_timeout = $config['timeout']; | |
95 | } | |
96 | if (isset($config['prefix'])) { | |
97 | $this->_prefix = $config['prefix']; | |
98 | } | |
99 | ||
100 | $this->_cache = new Memcache(); | |
101 | ||
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 Memcached server'; | |
105 | CRM_Utils_System::civiExit(); | |
106 | } | |
107 | } | |
108 | ||
5bc392e6 EM |
109 | /** |
110 | * @param $key | |
111 | * @param $value | |
112 | * | |
113 | * @return bool | |
114 | */ | |
00be9182 | 115 | public function set($key, &$value) { |
6a488035 TO |
116 | if (!$this->_cache->set($this->_prefix . $key, $value, FALSE, $this->_timeout)) { |
117 | return FALSE; | |
118 | } | |
119 | return TRUE; | |
120 | } | |
121 | ||
5bc392e6 EM |
122 | /** |
123 | * @param $key | |
124 | * | |
125 | * @return mixed | |
126 | */ | |
00be9182 | 127 | public function &get($key) { |
6a488035 TO |
128 | $result = $this->_cache->get($this->_prefix . $key); |
129 | return $result; | |
130 | } | |
131 | ||
5bc392e6 EM |
132 | /** |
133 | * @param $key | |
134 | * | |
135 | * @return mixed | |
136 | */ | |
00be9182 | 137 | public function delete($key) { |
6a488035 TO |
138 | return $this->_cache->delete($this->_prefix . $key); |
139 | } | |
140 | ||
5bc392e6 EM |
141 | /** |
142 | * @return mixed | |
143 | */ | |
00be9182 | 144 | public function flush() { |
6a488035 TO |
145 | return $this->_cache->flush(); |
146 | } | |
96025800 | 147 | |
6a488035 | 148 | } |