]> 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
56 class MemcachedPlugin extends Plugin
57 {
58     static $cacheInitialized = false;
59
60     private $_conn  = null;
61     public $servers = array('127.0.0.1;11211');
62
63     public $defaultExpiry = 86400; // 24h
64
65     /**
66      * Initialize the plugin
67      *
68      * Note that onStartCacheGet() may have been called before this!
69      *
70      * @return boolean flag value
71      */
72     function onInitializePlugin()
73     {
74         $this->_ensureConn();
75         self::$cacheInitialized = true;
76         return true;
77     }
78
79     /**
80      * Get a value associated with a key
81      *
82      * The value should have been set previously.
83      *
84      * @param string &$key   in; Lookup key
85      * @param mixed  &$value out; value associated with key
86      *
87      * @return boolean hook success
88      */
89     function onStartCacheGet(&$key, &$value)
90     {
91         $this->_ensureConn();
92         $value = $this->_conn->get($key);
93         Event::handle('EndCacheGet', array($key, &$value));
94         return false;
95     }
96
97     /**
98      * Associate a value with a key
99      *
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
105      *
106      * @return boolean hook success
107      */
108     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
109     {
110         $this->_ensureConn();
111         if ($expiry === null) {
112             $expiry = $this->defaultExpiry;
113         }
114         $success = $this->_conn->set($key, $value, $expiry);
115         Event::handle('EndCacheSet', array($key, $value, $flag,
116                                            $expiry));
117         return false;
118     }
119
120     /**
121      * Atomically increment an existing numeric key value.
122      * Existing expiration time will not be changed.
123      *
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.
127      *
128      * @return boolean hook success
129      */
130     function onStartCacheIncrement(&$key, &$step, &$value)
131     {
132         $this->_ensureConn();
133         $value = $this->_conn->increment($key, $step);
134         Event::handle('EndCacheIncrement', array($key, $step, $value));
135         return false;
136     }
137
138     /**
139      * Delete a value associated with a key
140      *
141      * @param string  &$key     in; Key to lookup
142      * @param boolean &$success out; whether it worked
143      *
144      * @return boolean hook success
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     private function _ensureConn()
169     {
170         if (empty($this->_conn)) {
171             $this->_conn = new Memcached(common_config('site', 'nickname'));
172
173             if (!count($this->_conn->getServerList())) {
174             if (is_array($this->servers)) {
175                 $servers = $this->servers;
176             } else {
177                 $servers = array($this->servers);
178             }
179             foreach ($servers as $server) {
180                 if (strpos($server, ';') !== false) {
181                     list($host, $port) = explode(';', $server);
182                 } else {
183                     $host = $server;
184                     $port = 11211;
185                 }
186
187                 $this->_conn->addServer($host, $port);
188             }
189
190             // Compress items stored in the cache.
191
192             // Allows the cache to store objects larger than 1MB (if they
193             // compress to less than 1MB), and improves cache memory efficiency.
194
195             $this->_conn->setOption(Memcached::OPT_COMPRESSION, true);
196             }
197         }
198     }
199
200     /**
201      * Translate general flags to Memcached-specific flags
202      * @param int $flag
203      * @return int
204      */
205     protected function flag($flag)
206     {
207         //no flags are presently supported
208         return $flag;
209     }
210
211     function onPluginVersion(&$versions)
212     {
213         $versions[] = array('name' => 'Memcached',
214                             'version' => STATUSNET_VERSION,
215                             'author' => 'Evan Prodromou, Craig Andrews',
216                             'homepage' => 'http://status.net/wiki/Plugin:Memcached',
217                             'rawdescription' =>
218                             _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
219         return true;
220     }
221 }