(dev/core#174) CRM_Utils_Cache_Interface::delete() - Return bool like PSR-16
[civicrm-core.git] / CRM / Utils / Cache / Memcache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 */
33 class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface {
34
35 use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation.
36
37 const DEFAULT_HOST = 'localhost';
38 const DEFAULT_PORT = 11211;
39 const DEFAULT_TIMEOUT = 3600;
40 const DEFAULT_PREFIX = '';
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 Memcache
78 */
79 protected $_cache;
80
81 /**
82 * Constructor.
83 *
84 * @param array $config
85 * An array of configuration params.
86 *
87 * @return \CRM_Utils_Cache_Memcache
88 */
89 public function __construct($config) {
90 if (isset($config['host'])) {
91 $this->_host = $config['host'];
92 }
93 if (isset($config['port'])) {
94 $this->_port = $config['port'];
95 }
96 if (isset($config['timeout'])) {
97 $this->_timeout = $config['timeout'];
98 }
99 if (isset($config['prefix'])) {
100 $this->_prefix = $config['prefix'];
101 }
102
103 $this->_cache = new Memcache();
104
105 if (!$this->_cache->connect($this->_host, $this->_port)) {
106 // dont use fatal here since we can go in an infinite loop
107 echo 'Could not connect to Memcached server';
108 CRM_Utils_System::civiExit();
109 }
110 }
111
112 /**
113 * @param $key
114 * @param $value
115 * @param null|int|\DateInterval $ttl
116 *
117 * @return bool
118 */
119 public function set($key, $value, $ttl = NULL) {
120 if ($ttl !== NULL) {
121 throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL");
122 }
123 if (!$this->_cache->set($this->_prefix . $key, $value, FALSE, $this->_timeout)) {
124 return FALSE;
125 }
126 return TRUE;
127 }
128
129 /**
130 * @param $key
131 * @param mixed $default
132 *
133 * @return mixed
134 */
135 public function get($key, $default = NULL) {
136 if ($default !== NULL) {
137 throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default");
138 }
139 $result = $this->_cache->get($this->_prefix . $key);
140 return $result;
141 }
142
143 /**
144 * @param $key
145 *
146 * @return bool
147 */
148 public function delete($key) {
149 return $this->_cache->delete($this->_prefix . $key);
150 }
151
152 /**
153 * @return bool
154 */
155 public function flush() {
156 // FIXME: Only delete items matching `$this->_prefix`.
157 return $this->_cache->flush();
158 }
159
160 }