]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MemcachePlugin.php
queue daemon fixes: path fix for xmpp, suppress warning in memcached init
[quix0rs-gnu-social.git] / plugins / MemcachePlugin.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 memcache
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  * @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 memcache 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>
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 MemcachePlugin extends Plugin
53 {
54     private $_conn  = null;
55     public $servers = array('127.0.0.1;11211');
56
57     public $compressThreshold = 20480;
58     public $compressMinSaving = 0.2;
59
60     public $persistent = null;
61
62     /**
63      * Initialize the plugin
64      *
65      * Note that onStartCacheGet() may have been called before this!
66      *
67      * @return boolean flag value
68      */
69
70     function onInitializePlugin()
71     {
72         if (is_null($this->persistent)) {
73             $this->persistent = (php_sapi_name() == 'cli') ? false : true;
74         }
75         $this->_ensureConn();
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
90     function onStartCacheGet(&$key, &$value)
91     {
92         $this->_ensureConn();
93         $value = $this->_conn->get($key);
94         Event::handle('EndCacheGet', array($key, &$value));
95         return false;
96     }
97
98     /**
99      * Associate a value with a key
100      *
101      * @param string  &$key     in; Key to use for lookups
102      * @param mixed   &$value   in; Value to associate
103      * @param integer &$flag    in; Flag (passed through to Memcache)
104      * @param integer &$expiry  in; Expiry (passed through to Memcache)
105      * @param boolean &$success out; Whether the set was successful
106      *
107      * @return boolean hook success
108      */
109
110     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
111     {
112         $this->_ensureConn();
113         $success = $this->_conn->set($key, $value, $flag, $expiry);
114         Event::handle('EndCacheSet', array($key, $value, $flag,
115                                            $expiry));
116         return false;
117     }
118
119     /**
120      * Delete a value associated with a key
121      *
122      * @param string  &$key     in; Key to lookup
123      * @param boolean &$success out; whether it worked
124      *
125      * @return boolean hook success
126      */
127
128     function onStartCacheDelete(&$key, &$success)
129     {
130         $this->_ensureConn();
131         $success = $this->_conn->delete($key);
132         Event::handle('EndCacheDelete', array($key));
133         return false;
134     }
135
136     function onStartCacheReconnect(&$success)
137     {
138         if (empty($this->_conn)) {
139             // nothing to do
140             return true;
141         }
142         if ($this->persistent) {
143             common_log(LOG_ERR, "Cannot close persistent memcached connection");
144             $success = false;
145         } else {
146             common_log(LOG_INFO, "Closing memcached connection");
147             $success = $this->_conn->close();
148             $this->_conn = null;
149         }
150         return false;
151     }
152
153     /**
154      * Ensure that a connection exists
155      *
156      * Checks the instance $_conn variable and connects
157      * if it is empty.
158      *
159      * @return void
160      */
161
162     private function _ensureConn()
163     {
164         if (empty($this->_conn)) {
165             $this->_conn = new Memcache();
166
167             if (is_array($this->servers)) {
168                 foreach ($this->servers as $server) {
169                     list($host, $port) = @explode(';', $server);
170                     if (empty($port)) {
171                         $port = 11211;
172                     }
173
174                     $this->_conn->addServer($host, $port, $this->persistent);
175                 }
176             } else {
177                 $this->_conn->addServer($this->servers, $this->persistent);
178                 list($host, $port) = explode(';', $this->servers);
179                 if (empty($port)) {
180                     $port = 11211;
181                 }
182                 $this->_conn->addServer($host, $port, $this->persistent);
183             }
184
185             // Compress items stored in the cache if they're over threshold in size
186             // (default 2KiB) and the compression would save more than min savings
187             // ratio (default 0.2).
188
189             // Allows the cache to store objects larger than 1MB (if they
190             // compress to less than 1MB), and improves cache memory efficiency.
191
192             $this->_conn->setCompressThreshold($this->compressThreshold,
193                                                $this->compressMinSaving);
194         }
195     }
196
197     function onPluginVersion(&$versions)
198     {
199         $versions[] = array('name' => 'Memcache',
200                             'version' => STATUSNET_VERSION,
201                             'author' => 'Evan Prodromou, Craig Andrews',
202                             'homepage' => 'http://status.net/wiki/Plugin:Memcache',
203                             'rawdescription' =>
204                             _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
205         return true;
206     }
207 }
208