]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MemcachedPlugin.php
Merge branch 'sitemap' of gitorious.org:~evan/statusnet/evans-mainline into sitemap
[quix0rs-gnu-social.git] / plugins / MemcachedPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
6  * Plugin to implement cache interface for memcached
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  Cache
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>, Craig Andrews <candrews@integralblue.com>
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * A plugin to use memcached for the cache interface
39  *
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.
43  *
44  * @category  Cache
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@status.net>, Craig Andrews <candrews@integralblue.com>
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/
50  */
51
52 class MemcachedPlugin extends Plugin
53 {
54     static $cacheInitialized = false;
55
56     private $_conn  = null;
57     public $servers = array('127.0.0.1;11211');
58
59     public $defaultExpiry = 86400; // 24h
60
61     /**
62      * Initialize the plugin
63      *
64      * Note that onStartCacheGet() may have been called before this!
65      *
66      * @return boolean flag value
67      */
68
69     function onInitializePlugin()
70     {
71         $this->_ensureConn();
72         self::$cacheInitialized = true;
73         return true;
74     }
75
76     /**
77      * Get a value associated with a key
78      *
79      * The value should have been set previously.
80      *
81      * @param string &$key   in; Lookup key
82      * @param mixed  &$value out; value associated with key
83      *
84      * @return boolean hook success
85      */
86
87     function onStartCacheGet(&$key, &$value)
88     {
89         $this->_ensureConn();
90         $value = $this->_conn->get($key);
91         Event::handle('EndCacheGet', array($key, &$value));
92         return false;
93     }
94
95     /**
96      * Associate a value with a key
97      *
98      * @param string  &$key     in; Key to use for lookups
99      * @param mixed   &$value   in; Value to associate
100      * @param integer &$flag    in; Flag empty or Cache::COMPRESSED
101      * @param integer &$expiry  in; Expiry (passed through to Memcache)
102      * @param boolean &$success out; Whether the set was successful
103      *
104      * @return boolean hook success
105      */
106
107     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
108     {
109         $this->_ensureConn();
110         if ($expiry === null) {
111             $expiry = $this->defaultExpiry;
112         }
113         $success = $this->_conn->set($key, $value, $expiry);
114         Event::handle('EndCacheSet', array($key, $value, $flag,
115                                            $expiry));
116         return false;
117     }
118
119     /**
120      * Atomically increment an existing numeric key value.
121      * Existing expiration time will not be changed.
122      *
123      * @param string &$key    in; Key to use for lookups
124      * @param int    &$step   in; Amount to increment (default 1)
125      * @param mixed  &$value  out; Incremented value, or false if key not set.
126      *
127      * @return boolean hook success
128      */
129     function onStartCacheIncrement(&$key, &$step, &$value)
130     {
131         $this->_ensureConn();
132         $value = $this->_conn->increment($key, $step);
133         Event::handle('EndCacheIncrement', array($key, $step, $value));
134         return false;
135     }
136
137     /**
138      * Delete a value associated with a key
139      *
140      * @param string  &$key     in; Key to lookup
141      * @param boolean &$success out; whether it worked
142      *
143      * @return boolean hook success
144      */
145
146     function onStartCacheDelete(&$key, &$success)
147     {
148         $this->_ensureConn();
149         $success = $this->_conn->delete($key);
150         Event::handle('EndCacheDelete', array($key));
151         return false;
152     }
153
154     function onStartCacheReconnect(&$success)
155     {
156         // nothing to do
157         return true;
158     }
159
160     /**
161      * Ensure that a connection exists
162      *
163      * Checks the instance $_conn variable and connects
164      * if it is empty.
165      *
166      * @return void
167      */
168
169     private function _ensureConn()
170     {
171         if (empty($this->_conn)) {
172             $this->_conn = new Memcached(common_config('site', 'nickname'));
173
174             if (!count($this->_conn->getServerList())) {
175             if (is_array($this->servers)) {
176                 $servers = $this->servers;
177             } else {
178                 $servers = array($this->servers);
179             }
180             foreach ($servers as $server) {
181                 if (strpos($server, ';') !== false) {
182                     list($host, $port) = explode(';', $server);
183                 } else {
184                     $host = $server;
185                     $port = 11211;
186                 }
187
188                 $this->_conn->addServer($host, $port);
189             }
190
191             // Compress items stored in the cache.
192
193             // Allows the cache to store objects larger than 1MB (if they
194             // compress to less than 1MB), and improves cache memory efficiency.
195
196             $this->_conn->setOption(Memcached::OPT_COMPRESSION, true);
197             }
198         }
199     }
200
201     /**
202      * Translate general flags to Memcached-specific flags
203      * @param int $flag
204      * @return int
205      */
206     protected function flag($flag)
207     {
208         //no flags are presently supported
209         return $flag;
210     }
211
212     function onPluginVersion(&$versions)
213     {
214         $versions[] = array('name' => 'Memcached',
215                             'version' => STATUSNET_VERSION,
216                             'author' => 'Evan Prodromou, Craig Andrews',
217                             'homepage' => 'http://status.net/wiki/Plugin:Memcached',
218                             'rawdescription' =>
219                             _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
220         return true;
221     }
222 }
223