]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Memcached/MemcachedPlugin.php
Localisation updates from http://translatewiki.net.
[quix0rs-gnu-social.git] / plugins / Memcached / 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>
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/
31  */
32
33 if (!defined('STATUSNET')) {
34     // This check helps protect against security problems;
35     // your code file can't be executed directly from the web.
36     exit(1);
37 }
38
39 /**
40  * A plugin to use memcached for the cache interface
41  *
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.
45  *
46  * @category  Cache
47  * @package   StatusNet
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/
54  */
55 class MemcachedPlugin extends Plugin
56 {
57     static $cacheInitialized = false;
58
59     private $_conn  = null;
60     public $servers = array('127.0.0.1;11211');
61
62     public $defaultExpiry = 86400; // 24h
63
64     /**
65      * Initialize the plugin
66      *
67      * Note that onStartCacheGet() may have been called before this!
68      *
69      * @return boolean flag value
70      */
71     function onInitializePlugin()
72     {
73         $this->_ensureConn();
74         self::$cacheInitialized = true;
75         return true;
76     }
77
78     /**
79      * Get a value associated with a key
80      *
81      * The value should have been set previously.
82      *
83      * @param string &$key   in; Lookup key
84      * @param mixed  &$value out; value associated with key
85      *
86      * @return boolean hook success
87      */
88     function onStartCacheGet(&$key, &$value)
89     {
90         $this->_ensureConn();
91         $value = $this->_conn->get($key);
92         Event::handle('EndCacheGet', array($key, &$value));
93         return false;
94     }
95
96     /**
97      * Associate a value with a key
98      *
99      * @param string  &$key     in; Key to use for lookups
100      * @param mixed   &$value   in; Value to associate
101      * @param integer &$flag    in; Flag empty or Cache::COMPRESSED
102      * @param integer &$expiry  in; Expiry (passed through to Memcache)
103      * @param boolean &$success out; Whether the set was successful
104      *
105      * @return boolean hook success
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     function onStartCacheDelete(&$key, &$success)
146     {
147         $this->_ensureConn();
148         $success = $this->_conn->delete($key);
149         Event::handle('EndCacheDelete', array($key));
150         return false;
151     }
152
153     function onStartCacheReconnect(&$success)
154     {
155         // nothing to do
156         return true;
157     }
158
159     /**
160      * Ensure that a connection exists
161      *
162      * Checks the instance $_conn variable and connects
163      * if it is empty.
164      *
165      * @return void
166      */
167     private function _ensureConn()
168     {
169         if (empty($this->_conn)) {
170             $this->_conn = new Memcached(common_config('site', 'nickname'));
171
172             if (!count($this->_conn->getServerList())) {
173             if (is_array($this->servers)) {
174                 $servers = $this->servers;
175             } else {
176                 $servers = array($this->servers);
177             }
178             foreach ($servers as $server) {
179                 if (strpos($server, ';') !== false) {
180                     list($host, $port) = explode(';', $server);
181                 } else {
182                     $host = $server;
183                     $port = 11211;
184                 }
185
186                 $this->_conn->addServer($host, $port);
187             }
188
189             // Compress items stored in the cache.
190
191             // Allows the cache to store objects larger than 1MB (if they
192             // compress to less than 1MB), and improves cache memory efficiency.
193
194             $this->_conn->setOption(Memcached::OPT_COMPRESSION, true);
195             }
196         }
197     }
198
199     /**
200      * Translate general flags to Memcached-specific flags
201      * @param int $flag
202      * @return int
203      */
204     protected function flag($flag)
205     {
206         //no flags are presently supported
207         return $flag;
208     }
209
210     function onPluginVersion(&$versions)
211     {
212         $versions[] = array('name' => 'Memcached',
213                             'version' => STATUSNET_VERSION,
214                             'author' => 'Evan Prodromou, Craig Andrews',
215                             'homepage' => 'http://status.net/wiki/Plugin:Memcached',
216                             'rawdescription' =>
217                             // TRANS: Plugin description.
218                             _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
219         return true;
220     }
221 }