]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MemcachePlugin.php
Merge branch '0.9.x' of git@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     /**
58      * Initialize the plugin
59      *
60      * Note that onStartCacheGet() may have been called before this!
61      *
62      * @return boolean flag value
63      */
64
65     function onInitializePlugin()
66     {
67         $this->_ensureConn();
68         return true;
69     }
70
71     /**
72      * Get a value associated with a key
73      *
74      * The value should have been set previously.
75      *
76      * @param string &$key   in; Lookup key
77      * @param mixed  &$value out; value associated with key
78      *
79      * @return boolean hook success
80      */
81
82     function onStartCacheGet(&$key, &$value)
83     {
84         $this->_ensureConn();
85         $value = $this->_conn->get($key);
86         Event::handle('EndCacheGet', array($key, &$value));
87         return false;
88     }
89
90     /**
91      * Associate a value with a key
92      *
93      * @param string  &$key     in; Key to use for lookups
94      * @param mixed   &$value   in; Value to associate
95      * @param integer &$flag    in; Flag (passed through to Memcache)
96      * @param integer &$expiry  in; Expiry (passed through to Memcache)
97      * @param boolean &$success out; Whether the set was successful
98      *
99      * @return boolean hook success
100      */
101
102     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
103     {
104         $this->_ensureConn();
105         $success = $this->_conn->set($key, $value, $flag, $expiry);
106         Event::handle('EndCacheSet', array($key, $value, $flag,
107                                            $expiry));
108         return false;
109     }
110
111     /**
112      * Delete a value associated with a key
113      *
114      * @param string  &$key     in; Key to lookup
115      * @param boolean &$success out; whether it worked
116      *
117      * @return boolean hook success
118      */
119
120     function onStartCacheDelete(&$key, &$success)
121     {
122         $this->_ensureConn();
123         $success = $this->_conn->delete($key);
124         Event::handle('EndCacheDelete', array($key));
125         return false;
126     }
127
128     /**
129      * Ensure that a connection exists
130      *
131      * Checks the instance $_conn variable and connects
132      * if it is empty.
133      *
134      * @return void
135      */
136
137     private function _ensureConn()
138     {
139         if (empty($this->_conn)) {
140             $this->_conn = new Memcache();
141
142             if (is_array($this->servers)) {
143                 foreach ($this->servers as $server) {
144                     list($host, $port) = explode(';', $server);
145                     if (empty($port)) {
146                         $port = 11211;
147                     }
148
149                     $this->_conn->addServer($host, $port);
150                 }
151             } else {
152                 $this->_conn->addServer($this->servers);
153                 list($host, $port) = explode(';', $this->servers);
154                 if (empty($port)) {
155                     $port = 11211;
156                 }
157                 $this->_conn->addServer($host, $port);
158             }
159         }
160     }
161 }
162