3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * Plugin to implement cache interface for memcache
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * A plugin to use memcache for the cache interface
40 * This used to be encoded as config-variable options in the core code;
41 * it's now broken out to a separate plugin. The same interface can be
42 * implemented by other plugins.
46 * @author Evan Prodromou <evan@status.net>
47 * @copyright 2009 StatusNet, Inc.
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49 * @link http://status.net/
51 class MemcachePlugin extends Plugin
53 static $cacheInitialized = false;
55 private $_conn = null;
56 public $servers = array('127.0.0.1;11211');
58 public $compressThreshold = 20480;
59 public $compressMinSaving = 0.2;
61 public $persistent = null;
63 public $defaultExpiry = 86400; // 24h
66 * Initialize the plugin
68 * Note that onStartCacheGet() may have been called before this!
70 * @return boolean flag value
72 function onInitializePlugin()
74 if (self::$cacheInitialized) {
75 $this->persistent = true;
77 // If we're a parent command-line process we need
78 // to be able to close out the connection after
79 // forking, so disable persistence.
81 // We'll turn it back on again the second time
82 // through which will either be in a child process,
83 // or a single-process script which is switching
85 $this->persistent = (php_sapi_name() == 'cli') ? false : true;
88 self::$cacheInitialized = true;
93 * Get a value associated with a key
95 * The value should have been set previously.
97 * @param string &$key in; Lookup key
98 * @param mixed &$value out; value associated with key
100 * @return boolean hook success
102 function onStartCacheGet(&$key, &$value)
104 $this->_ensureConn();
105 $value = $this->_conn->get($key);
106 Event::handle('EndCacheGet', array($key, &$value));
111 * Associate a value with a key
113 * @param string &$key in; Key to use for lookups
114 * @param mixed &$value in; Value to associate
115 * @param integer &$flag in; Flag empty or Cache::COMPRESSED
116 * @param integer &$expiry in; Expiry (passed through to Memcache)
117 * @param boolean &$success out; Whether the set was successful
119 * @return boolean hook success
121 function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
123 $this->_ensureConn();
124 if ($expiry === null) {
125 $expiry = $this->defaultExpiry;
127 $success = $this->_conn->set($key, $value, $this->flag(intval($flag)), $expiry);
128 Event::handle('EndCacheSet', array($key, $value, $flag,
134 * Atomically increment an existing numeric key value.
135 * Existing expiration time will not be changed.
137 * @param string &$key in; Key to use for lookups
138 * @param int &$step in; Amount to increment (default 1)
139 * @param mixed &$value out; Incremented value, or false if key not set.
141 * @return boolean hook success
143 function onStartCacheIncrement(&$key, &$step, &$value)
145 $this->_ensureConn();
146 $value = $this->_conn->increment($key, $step);
147 Event::handle('EndCacheIncrement', array($key, $step, $value));
152 * Delete a value associated with a key
154 * @param string &$key in; Key to lookup
155 * @param boolean &$success out; whether it worked
157 * @return boolean hook success
159 function onStartCacheDelete(&$key, &$success)
161 $this->_ensureConn();
162 $success = $this->_conn->delete($key);
163 Event::handle('EndCacheDelete', array($key));
167 function onStartCacheReconnect(&$success)
169 if (empty($this->_conn)) {
173 if ($this->persistent) {
174 common_log(LOG_ERR, "Cannot close persistent memcached connection");
177 common_log(LOG_INFO, "Closing memcached connection");
178 $success = $this->_conn->close();
185 * Ensure that a connection exists
187 * Checks the instance $_conn variable and connects
192 private function _ensureConn()
194 if (empty($this->_conn)) {
195 $this->_conn = new Memcache();
197 if (is_array($this->servers)) {
198 $servers = $this->servers;
200 $servers = array($this->servers);
202 foreach ($servers as $server) {
203 if (strpos($server, ';') !== false) {
204 list($host, $port) = explode(';', $server);
210 $this->_conn->addServer($host, $port, $this->persistent);
213 // Compress items stored in the cache if they're over threshold in size
214 // (default 2KiB) and the compression would save more than min savings
215 // ratio (default 0.2).
217 // Allows the cache to store objects larger than 1MB (if they
218 // compress to less than 1MB), and improves cache memory efficiency.
220 $this->_conn->setCompressThreshold($this->compressThreshold,
221 $this->compressMinSaving);
226 * Translate general flags to Memcached-specific flags
230 protected function flag($flag)
233 if ($flag & Cache::COMPRESSED == Cache::COMPRESSED) {
234 $out |= MEMCACHE_COMPRESSED;
239 function onPluginVersion(array &$versions)
241 $versions[] = array('name' => 'Memcache',
242 'version' => GNUSOCIAL_VERSION,
243 'author' => 'Evan Prodromou, Craig Andrews',
244 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Memcache',
246 // TRANS: Plugin description.
247 _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));