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;
61 * Initialize the plugin
63 * Note that onStartCacheGet() may have been called before this!
65 * @return boolean flag value
68 function onInitializePlugin()
75 * Get a value associated with a key
77 * The value should have been set previously.
79 * @param string &$key in; Lookup key
80 * @param mixed &$value out; value associated with key
82 * @return boolean hook success
85 function onStartCacheGet(&$key, &$value)
88 $value = $this->_conn->get($key);
89 Event::handle('EndCacheGet', array($key, &$value));
94 * Associate a value with a key
96 * @param string &$key in; Key to use for lookups
97 * @param mixed &$value in; Value to associate
98 * @param integer &$flag in; Flag (passed through to Memcache)
99 * @param integer &$expiry in; Expiry (passed through to Memcache)
100 * @param boolean &$success out; Whether the set was successful
102 * @return boolean hook success
105 function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
107 $this->_ensureConn();
108 $success = $this->_conn->set($key, $value, $flag, $expiry);
109 Event::handle('EndCacheSet', array($key, $value, $flag,
115 * Delete a value associated with a key
117 * @param string &$key in; Key to lookup
118 * @param boolean &$success out; whether it worked
120 * @return boolean hook success
123 function onStartCacheDelete(&$key, &$success)
125 $this->_ensureConn();
126 $success = $this->_conn->delete($key);
127 Event::handle('EndCacheDelete', array($key));
132 * Ensure that a connection exists
134 * Checks the instance $_conn variable and connects
140 private function _ensureConn()
142 if (empty($this->_conn)) {
143 $this->_conn = new Memcache();
145 if (is_array($this->servers)) {
146 foreach ($this->servers as $server) {
147 list($host, $port) = explode(';', $server);
152 $this->_conn->addServer($host, $port);
155 $this->_conn->addServer($this->servers);
156 list($host, $port) = explode(';', $this->servers);
160 $this->_conn->addServer($host, $port);
163 // Compress items stored in the cache if they're over threshold in size
164 // (default 2KiB) and the compression would save more than min savings
165 // ratio (default 0.2).
167 // Allows the cache to store objects larger than 1MB (if they
168 // compress to less than 1MB), and improves cache memory efficiency.
170 $this->_conn->setCompressThreshold($this->compressThreshold,
171 $this->compressMinSaving);