Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Cache_Memcache { | |
353ffa53 TO |
36 | const DEFAULT_HOST = 'localhost'; |
37 | const DEFAULT_PORT = 11211; | |
6a488035 | 38 | const DEFAULT_TIMEOUT = 3600; |
353ffa53 | 39 | const DEFAULT_PREFIX = ''; |
6a488035 TO |
40 | |
41 | /** | |
fe482240 | 42 | * The host name of the memcached server. |
6a488035 TO |
43 | * |
44 | * @var string | |
45 | */ | |
46 | protected $_host = self::DEFAULT_HOST; | |
47 | ||
48 | /** | |
fe482240 | 49 | * The port on which to connect on. |
6a488035 TO |
50 | * |
51 | * @var int | |
52 | */ | |
53 | protected $_port = self::DEFAULT_PORT; | |
54 | ||
55 | /** | |
fe482240 | 56 | * The default timeout to use. |
6a488035 TO |
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 memcache 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 | /** | |
fe482240 | 74 | * The actual memcache object. |
6a488035 TO |
75 | * |
76 | * @var resource | |
77 | */ | |
78 | protected $_cache; | |
79 | ||
80 | /** | |
fe482240 | 81 | * Constructor. |
6a488035 | 82 | * |
77855840 TO |
83 | * @param array $config |
84 | * An array of configuration params. | |
6a488035 | 85 | * |
77b97be7 | 86 | * @return \CRM_Utils_Cache_Memcache |
6a488035 | 87 | */ |
00be9182 | 88 | public function __construct($config) { |
6a488035 TO |
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 Memcache(); | |
103 | ||
104 | if (!$this->_cache->connect($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 | */ | |
00be9182 | 117 | public function set($key, &$value) { |
6a488035 TO |
118 | if (!$this->_cache->set($this->_prefix . $key, $value, FALSE, $this->_timeout)) { |
119 | return FALSE; | |
120 | } | |
121 | return TRUE; | |
122 | } | |
123 | ||
5bc392e6 EM |
124 | /** |
125 | * @param $key | |
126 | * | |
127 | * @return mixed | |
128 | */ | |
00be9182 | 129 | public function &get($key) { |
6a488035 TO |
130 | $result = $this->_cache->get($this->_prefix . $key); |
131 | return $result; | |
132 | } | |
133 | ||
5bc392e6 EM |
134 | /** |
135 | * @param $key | |
136 | * | |
137 | * @return mixed | |
138 | */ | |
00be9182 | 139 | public function delete($key) { |
6a488035 TO |
140 | return $this->_cache->delete($this->_prefix . $key); |
141 | } | |
142 | ||
5bc392e6 EM |
143 | /** |
144 | * @return mixed | |
145 | */ | |
00be9182 | 146 | public function flush() { |
6a488035 TO |
147 | return $this->_cache->flush(); |
148 | } | |
96025800 | 149 | |
6a488035 | 150 | } |