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/
52 class MemcachePlugin extends Plugin
54 private $_conn = null;
55 public $servers = array('127.0.0.1;11211');
57 public $compressThreshold = 20480;
58 public $compressMinSaving = 0.2;
60 public $persistent = null;
62 public $defaultExpiry = 86400; // 24h
65 * Initialize the plugin
67 * Note that onStartCacheGet() may have been called before this!
69 * @return boolean flag value
72 function onInitializePlugin()
74 if (is_null($this->persistent)) {
75 $this->persistent = (php_sapi_name() == 'cli') ? false : true;
82 * Get a value associated with a key
84 * The value should have been set previously.
86 * @param string &$key in; Lookup key
87 * @param mixed &$value out; value associated with key
89 * @return boolean hook success
92 function onStartCacheGet(&$key, &$value)
95 $value = $this->_conn->get($key);
96 Event::handle('EndCacheGet', array($key, &$value));
101 * Associate a value with a key
103 * @param string &$key in; Key to use for lookups
104 * @param mixed &$value in; Value to associate
105 * @param integer &$flag in; Flag empty or Cache::COMPRESSED
106 * @param integer &$expiry in; Expiry (passed through to Memcache)
107 * @param boolean &$success out; Whether the set was successful
109 * @return boolean hook success
112 function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
114 $this->_ensureConn();
115 if ($expiry === null) {
116 $expiry = $this->defaultExpiry;
118 $success = $this->_conn->set($key, $value, $this->flag(intval($flag)), $expiry);
119 Event::handle('EndCacheSet', array($key, $value, $flag,
125 * Delete a value associated with a key
127 * @param string &$key in; Key to lookup
128 * @param boolean &$success out; whether it worked
130 * @return boolean hook success
133 function onStartCacheDelete(&$key, &$success)
135 $this->_ensureConn();
136 $success = $this->_conn->delete($key);
137 Event::handle('EndCacheDelete', array($key));
141 function onStartCacheReconnect(&$success)
143 if (empty($this->_conn)) {
147 if ($this->persistent) {
148 common_log(LOG_ERR, "Cannot close persistent memcached connection");
151 common_log(LOG_INFO, "Closing memcached connection");
152 $success = $this->_conn->close();
159 * Ensure that a connection exists
161 * Checks the instance $_conn variable and connects
167 private function _ensureConn()
169 if (empty($this->_conn)) {
170 $this->_conn = new Memcache();
172 if (is_array($this->servers)) {
173 $servers = $this->servers;
175 $servers = array($this->servers);
177 foreach ($servers as $server) {
178 if (strpos($server, ';') !== false) {
179 list($host, $port) = explode(';', $server);
185 $this->_conn->addServer($host, $port, $this->persistent);
188 // Compress items stored in the cache if they're over threshold in size
189 // (default 2KiB) and the compression would save more than min savings
190 // ratio (default 0.2).
192 // Allows the cache to store objects larger than 1MB (if they
193 // compress to less than 1MB), and improves cache memory efficiency.
195 $this->_conn->setCompressThreshold($this->compressThreshold,
196 $this->compressMinSaving);
201 * Translate general flags to Memcached-specific flags
205 protected function flag($flag)
208 if ($flag & Cache::COMPRESSED == Cache::COMPRESSED) {
209 $out |= MEMCACHE_COMPRESSED;
214 function onPluginVersion(&$versions)
216 $versions[] = array('name' => 'Memcache',
217 'version' => STATUSNET_VERSION,
218 'author' => 'Evan Prodromou, Craig Andrews',
219 'homepage' => 'http://status.net/wiki/Plugin:Memcache',
221 _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));