3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
24 class Memcached_DataObject extends DB_DataObject
26 function &staticGet($cls, $k, $v=null)
36 $i = Memcached_DataObject::getcached($cls, $k, $v);
40 $i = DB_DataObject::staticGet($cls, $k, $v);
48 function &pkeyGet($cls, $kv)
50 $i = Memcached_DataObject::multicache($cls, $kv);
55 foreach ($kv as $k => $v) {
69 $result = parent::insert();
73 function update($orig=null)
75 if (is_object($orig) && $orig instanceof Memcached_DataObject) {
76 $orig->decache(); # might be different keys
78 $result = parent::update($orig);
87 $this->decache(); # while we still have the values!
88 return parent::delete();
91 static function memcache() {
92 return common_memcache();
95 static function cacheKey($cls, $k, $v) {
96 return common_cache_key(strtolower($cls).':'.$k.':'.$v);
99 static function getcached($cls, $k, $v) {
100 $c = Memcached_DataObject::memcache();
104 return $c->get(Memcached_DataObject::cacheKey($cls, $k, $v));
110 global $_DB_DATAOBJECT;
111 if (!isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) {
112 $this->databaseStructure();
115 return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"];
120 $c = $this->memcache();
126 $types = $this->keyTypes();
128 foreach ($types as $key => $type) {
131 $pval[] = $this->$key;
133 $c->set($this->cacheKey($this->tableName(), $key, $this->$key), $this);
136 # XXX: should work for both compound and scalar pkeys
137 $pvals = implode(',', $pval);
138 $pkeys = implode(',', $pkey);
139 $c->set($this->cacheKey($this->tableName(), $pkeys, $pvals), $this);
145 $c = $this->memcache();
151 $types = $this->keyTypes();
153 foreach ($types as $key => $type) {
156 $pval[] = $this->$key;
158 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
161 # should work for both compound and scalar pkeys
162 # XXX: comma works for now but may not be safe separator for future keys
163 $pvals = implode(',', $pval);
164 $pkeys = implode(',', $pkey);
165 $c->delete($this->cacheKey($this->tableName(), $pkeys, $pvals));
169 function multicache($cls, $kv)
172 $c = Memcached_DataObject::memcache();
176 $pkeys = implode(',', array_keys($kv));
177 $pvals = implode(',', array_values($kv));
178 return $c->get(Memcached_DataObject::cacheKey($cls, $pkeys, $pvals));
182 function getSearchEngine($table)
184 require_once INSTALLDIR.'/lib/search_engines.php';
185 static $search_engine;
186 if (!isset($search_engine)) {
187 if (Event::handle('GetSearchEngine', array($this, $table, &$search_engine))) {
188 if ('mysql' === common_config('db', 'type')) {
189 $type = common_config('search', 'type');
190 if ($type == 'like') {
191 $search_engine = new MySQLLikeSearch($this, $table);
192 } else if ($type == 'fulltext') {
193 $search_engine = new MySQLSearch($this, $table);
195 throw new ServerException('Unknown search type: ' . $type);
198 $search_engine = new PGSearch($this, $table);
202 return $search_engine;
205 static function cachedQuery($cls, $qry, $expiry=3600)
207 $c = Memcached_DataObject::memcache();
213 $key_part = common_keyize($cls).':'.md5($qry);
214 $ckey = common_cache_key($key_part);
215 $stored = $c->get($ckey);
217 return new ArrayWrapper($stored);
223 while ($inst->fetch()) {
224 $cached[] = clone($inst);
227 $c->set($ckey, $cached, MEMCACHE_COMPRESSED, $expiry);
228 return new ArrayWrapper($cached);
231 // We overload so that 'SET NAMES "utf8"' is called for
236 global $_DB_DATAOBJECT;
238 $sum = $this->_getDbDsnMD5();
240 if (!empty($_DB_DATAOBJECT['CONNECTIONS'][$sum]) &&
241 !PEAR::isError($_DB_DATAOBJECT['CONNECTIONS'][$sum])) {
247 $result = parent::_connect();
249 if ($result && !$exists) {
250 $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
251 if (common_config('db', 'type') == 'mysql' &&
252 common_config('db', 'utf8')) {
253 $conn = $DB->connection;
255 if ($DB instanceof DB_mysqli) {
256 mysqli_set_charset($conn, 'utf8');
257 } else if ($DB instanceof DB_mysql) {
258 mysql_set_charset('utf8', $conn);
267 // XXX: largely cadged from DB_DataObject
269 function _getDbDsnMD5()
271 if ($this->_database_dsn_md5) {
272 return $this->_database_dsn_md5;
275 $dsn = $this->_getDbDsn();
277 if (is_string($dsn)) {
280 /// support array based dsn's
281 $sum = md5(serialize($dsn));
289 global $_DB_DATAOBJECT;
291 if (empty($_DB_DATAOBJECT['CONFIG'])) {
292 DB_DataObject::_loadConfig();
295 $options = &$_DB_DATAOBJECT['CONFIG'];
297 // if the databse dsn dis defined in the object..
299 $dsn = isset($this->_database_dsn) ? $this->_database_dsn : null;
303 if (!$this->_database) {
304 $this->_database = isset($options["table_{$this->__table}"]) ? $options["table_{$this->__table}"] : null;
307 if ($this->_database && !empty($options["database_{$this->_database}"])) {
308 $dsn = $options["database_{$this->_database}"];
309 } else if (!empty($options['database'])) {
310 $dsn = $options['database'];
315 throw new Exception("No database name / dsn found anywhere");