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
27 * Destructor to free global memory resources associated with
28 * this data object when it's unset or goes out of scope.
29 * DB_DataObject doesn't do this yet by itself.
35 if (method_exists('DB_DataObject', '__destruct')) {
40 function &staticGet($cls, $k, $v=null)
50 $i = Memcached_DataObject::getcached($cls, $k, $v);
54 $i = DB_DataObject::factory($cls);
58 $result = $i->get($k, $v);
68 function &pkeyGet($cls, $kv)
70 $i = Memcached_DataObject::multicache($cls, $kv);
75 foreach ($kv as $k => $v) {
89 $result = parent::insert();
93 function update($orig=null)
95 if (is_object($orig) && $orig instanceof Memcached_DataObject) {
96 $orig->decache(); # might be different keys
98 $result = parent::update($orig);
107 $this->decache(); # while we still have the values!
108 return parent::delete();
111 static function memcache() {
112 return common_memcache();
115 static function cacheKey($cls, $k, $v) {
116 if (is_object($cls) || is_object($j) || is_object($v)) {
117 $e = new Exception();
118 common_log(LOG_ERR, __METHOD__ . ' object in param: ' .
119 str_replace("\n", " ", $e->getTraceAsString()));
121 return common_cache_key(strtolower($cls).':'.$k.':'.$v);
124 static function getcached($cls, $k, $v) {
125 $c = Memcached_DataObject::memcache();
129 return $c->get(Memcached_DataObject::cacheKey($cls, $k, $v));
135 global $_DB_DATAOBJECT;
136 if (!isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) {
137 $this->databaseStructure();
140 return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"];
145 $c = $this->memcache();
151 $types = $this->keyTypes();
153 foreach ($types as $key => $type) {
156 $pval[] = $this->$key;
158 $c->set($this->cacheKey($this->tableName(), $key, $this->$key), $this);
161 # XXX: should work for both compound and scalar pkeys
162 $pvals = implode(',', $pval);
163 $pkeys = implode(',', $pkey);
164 $c->set($this->cacheKey($this->tableName(), $pkeys, $pvals), $this);
170 $c = $this->memcache();
176 $types = $this->keyTypes();
178 foreach ($types as $key => $type) {
181 $pval[] = $this->$key;
183 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
186 # should work for both compound and scalar pkeys
187 # XXX: comma works for now but may not be safe separator for future keys
188 $pvals = implode(',', $pval);
189 $pkeys = implode(',', $pkey);
190 $c->delete($this->cacheKey($this->tableName(), $pkeys, $pvals));
194 function multicache($cls, $kv)
197 $c = Memcached_DataObject::memcache();
201 $pkeys = implode(',', array_keys($kv));
202 $pvals = implode(',', array_values($kv));
203 return $c->get(Memcached_DataObject::cacheKey($cls, $pkeys, $pvals));
207 function getSearchEngine($table)
209 require_once INSTALLDIR.'/lib/search_engines.php';
210 static $search_engine;
211 if (!isset($search_engine)) {
212 if (Event::handle('GetSearchEngine', array($this, $table, &$search_engine))) {
213 if ('mysql' === common_config('db', 'type')) {
214 $type = common_config('search', 'type');
215 if ($type == 'like') {
216 $search_engine = new MySQLLikeSearch($this, $table);
217 } else if ($type == 'fulltext') {
218 $search_engine = new MySQLSearch($this, $table);
220 throw new ServerException('Unknown search type: ' . $type);
223 $search_engine = new PGSearch($this, $table);
227 return $search_engine;
230 static function cachedQuery($cls, $qry, $expiry=3600)
232 $c = Memcached_DataObject::memcache();
238 $key_part = common_keyize($cls).':'.md5($qry);
239 $ckey = common_cache_key($key_part);
240 $stored = $c->get($ckey);
242 return new ArrayWrapper($stored);
248 while ($inst->fetch()) {
249 $cached[] = clone($inst);
252 $c->set($ckey, $cached, MEMCACHE_COMPRESSED, $expiry);
253 return new ArrayWrapper($cached);
256 // We overload so that 'SET NAMES "utf8"' is called for
261 global $_DB_DATAOBJECT;
263 $sum = $this->_getDbDsnMD5();
265 if (!empty($_DB_DATAOBJECT['CONNECTIONS'][$sum]) &&
266 !PEAR::isError($_DB_DATAOBJECT['CONNECTIONS'][$sum])) {
272 $result = parent::_connect();
274 if ($result && !$exists) {
275 $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
276 if (common_config('db', 'type') == 'mysql' &&
277 common_config('db', 'utf8')) {
278 $conn = $DB->connection;
280 if ($DB instanceof DB_mysqli) {
281 mysqli_set_charset($conn, 'utf8');
282 } else if ($DB instanceof DB_mysql) {
283 mysql_set_charset('utf8', $conn);
292 // XXX: largely cadged from DB_DataObject
294 function _getDbDsnMD5()
296 if ($this->_database_dsn_md5) {
297 return $this->_database_dsn_md5;
300 $dsn = $this->_getDbDsn();
302 if (is_string($dsn)) {
305 /// support array based dsn's
306 $sum = md5(serialize($dsn));
314 global $_DB_DATAOBJECT;
316 if (empty($_DB_DATAOBJECT['CONFIG'])) {
317 DB_DataObject::_loadConfig();
320 $options = &$_DB_DATAOBJECT['CONFIG'];
322 // if the databse dsn dis defined in the object..
324 $dsn = isset($this->_database_dsn) ? $this->_database_dsn : null;
328 if (!$this->_database) {
329 $this->_database = isset($options["table_{$this->__table}"]) ? $options["table_{$this->__table}"] : null;
332 if ($this->_database && !empty($options["database_{$this->_database}"])) {
333 $dsn = $options["database_{$this->_database}"];
334 } else if (!empty($options['database'])) {
335 $dsn = $options['database'];
340 throw new Exception("No database name / dsn found anywhere");