3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * Plugin to implement cache interface for memcached
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 * @author Craig Andrews <candrews@integralblue.com>
27 * @copyright 2009 StatusNet, Inc.
28 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
29 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30 * @link http://status.net/
33 if (!defined('STATUSNET')) {
34 // This check helps protect against security problems;
35 // your code file can't be executed directly from the web.
40 * A plugin to use memcached for the cache interface
42 * This used to be encoded as config-variable options in the core code;
43 * it's now broken out to a separate plugin. The same interface can be
44 * implemented by other plugins.
48 * @author Evan Prodromou <evan@status.net>
49 * @author Craig Andrews <candrews@integralblue.com>
50 * @copyright 2009 StatusNet, Inc.
51 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
52 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
53 * @link http://status.net/
56 class MemcachedPlugin extends Plugin
58 static $cacheInitialized = false;
60 private $_conn = null;
61 public $servers = array('127.0.0.1;11211');
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()
75 self::$cacheInitialized = true;
80 * Get a value associated with a key
82 * The value should have been set previously.
84 * @param string &$key in; Lookup key
85 * @param mixed &$value out; value associated with key
87 * @return boolean hook success
89 function onStartCacheGet(&$key, &$value)
92 $value = $this->_conn->get($key);
93 Event::handle('EndCacheGet', array($key, &$value));
98 * Associate a value with a key
100 * @param string &$key in; Key to use for lookups
101 * @param mixed &$value in; Value to associate
102 * @param integer &$flag in; Flag empty or Cache::COMPRESSED
103 * @param integer &$expiry in; Expiry (passed through to Memcache)
104 * @param boolean &$success out; Whether the set was successful
106 * @return boolean hook success
108 function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
110 $this->_ensureConn();
111 if ($expiry === null) {
112 $expiry = $this->defaultExpiry;
114 $success = $this->_conn->set($key, $value, $expiry);
115 Event::handle('EndCacheSet', array($key, $value, $flag,
121 * Atomically increment an existing numeric key value.
122 * Existing expiration time will not be changed.
124 * @param string &$key in; Key to use for lookups
125 * @param int &$step in; Amount to increment (default 1)
126 * @param mixed &$value out; Incremented value, or false if key not set.
128 * @return boolean hook success
130 function onStartCacheIncrement(&$key, &$step, &$value)
132 $this->_ensureConn();
133 $value = $this->_conn->increment($key, $step);
134 Event::handle('EndCacheIncrement', array($key, $step, $value));
139 * Delete a value associated with a key
141 * @param string &$key in; Key to lookup
142 * @param boolean &$success out; whether it worked
144 * @return boolean hook success
146 function onStartCacheDelete(&$key, &$success)
148 $this->_ensureConn();
149 $success = $this->_conn->delete($key);
150 Event::handle('EndCacheDelete', array($key));
154 function onStartCacheReconnect(&$success)
161 * Ensure that a connection exists
163 * Checks the instance $_conn variable and connects
168 private function _ensureConn()
170 if (empty($this->_conn)) {
171 $this->_conn = new Memcached(common_config('site', 'nickname'));
173 if (!count($this->_conn->getServerList())) {
174 if (is_array($this->servers)) {
175 $servers = $this->servers;
177 $servers = array($this->servers);
179 foreach ($servers as $server) {
180 if (strpos($server, ';') !== false) {
181 list($host, $port) = explode(';', $server);
187 $this->_conn->addServer($host, $port);
190 // Compress items stored in the cache.
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->setOption(Memcached::OPT_COMPRESSION, true);
201 * Translate general flags to Memcached-specific flags
205 protected function flag($flag)
207 //no flags are presently supported
211 function onPluginVersion(&$versions)
213 $versions[] = array('name' => 'Memcached',
214 'version' => STATUSNET_VERSION,
215 'author' => 'Evan Prodromou, Craig Andrews',
216 'homepage' => 'http://status.net/wiki/Plugin:Memcached',
218 _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));