]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MemcachePlugin.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x
[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     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
72     function onInitializePlugin()
73     {
74         if (is_null($this->persistent)) {
75             $this->persistent = (php_sapi_name() == 'cli') ? false : true;
76         }
77         $this->_ensureConn();
78         return true;
79     }
80
81     /**
82      * Get a value associated with a key
83      *
84      * The value should have been set previously.
85      *
86      * @param string &$key   in; Lookup key
87      * @param mixed  &$value out; value associated with key
88      *
89      * @return boolean hook success
90      */
91
92     function onStartCacheGet(&$key, &$value)
93     {
94         $this->_ensureConn();
95         $value = $this->_conn->get($key);
96         Event::handle('EndCacheGet', array($key, &$value));
97         return false;
98     }
99
100     /**
101      * Associate a value with a key
102      *
103      * @param string  &$key     in; Key to use for lookups
104      * @param mixed   &$value   in; Value to associate
105      * @param integer &$flag    in; Flag (passed through to Memcache)
106      * @param integer &$expiry  in; Expiry (passed through to Memcache)
107      * @param boolean &$success out; Whether the set was successful
108      *
109      * @return boolean hook success
110      */
111
112     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
113     {
114         $this->_ensureConn();
115         if ($expiry === null) {
116             $expiry = $this->defaultExpiry;
117         }
118         $success = $this->_conn->set($key, $value, $flag, $expiry);
119         Event::handle('EndCacheSet', array($key, $value, $flag,
120                                            $expiry));
121         return false;
122     }
123
124     /**
125      * Delete a value associated with a key
126      *
127      * @param string  &$key     in; Key to lookup
128      * @param boolean &$success out; whether it worked
129      *
130      * @return boolean hook success
131      */
132
133     function onStartCacheDelete(&$key, &$success)
134     {
135         $this->_ensureConn();
136         $success = $this->_conn->delete($key);
137         Event::handle('EndCacheDelete', array($key));
138         return false;
139     }
140
141     function onStartCacheReconnect(&$success)
142     {
143         if (empty($this->_conn)) {
144             // nothing to do
145             return true;
146         }
147         if ($this->persistent) {
148             common_log(LOG_ERR, "Cannot close persistent memcached connection");
149             $success = false;
150         } else {
151             common_log(LOG_INFO, "Closing memcached connection");
152             $success = $this->_conn->close();
153             $this->_conn = null;
154         }
155         return false;
156     }
157
158     /**
159      * Ensure that a connection exists
160      *
161      * Checks the instance $_conn variable and connects
162      * if it is empty.
163      *
164      * @return void
165      */
166
167     private function _ensureConn()
168     {
169         if (empty($this->_conn)) {
170             $this->_conn = new Memcache();
171
172             if (is_array($this->servers)) {
173                 $servers = $this->servers;
174             } else {
175                 $servers = array($this->servers);
176             }
177             foreach ($servers as $server) {
178                 if (strpos($server, ';') !== false) {
179                     list($host, $port) = explode(';', $server);
180                 } else {
181                     $host = $server;
182                     $port = 11211;
183                 }
184
185                 $this->_conn->addServer($host, $port, $this->persistent);
186             }
187
188             // Compress items stored in the cache if they're over threshold in size
189             // (default 2KiB) and the compression would save more than min savings
190             // ratio (default 0.2).
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->setCompressThreshold($this->compressThreshold,
196                                                $this->compressMinSaving);
197         }
198     }
199
200     function onPluginVersion(&$versions)
201     {
202         $versions[] = array('name' => 'Memcache',
203                             'version' => STATUSNET_VERSION,
204                             'author' => 'Evan Prodromou, Craig Andrews',
205                             'homepage' => 'http://status.net/wiki/Plugin:Memcache',
206                             'rawdescription' =>
207                             _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
208         return true;
209     }
210 }
211