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 if (is_object($cls) || is_object($j) || is_object($v)) {
98 common_log(LOG_ERR, __METHOD__ . ' object in param: ' .
99 str_replace("\n", " ", $e->getTraceAsString()));
101 return common_cache_key(strtolower($cls).':'.$k.':'.$v);
104 static function getcached($cls, $k, $v) {
105 $c = Memcached_DataObject::memcache();
109 return $c->get(Memcached_DataObject::cacheKey($cls, $k, $v));
115 global $_DB_DATAOBJECT;
116 if (!isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) {
117 $this->databaseStructure();
120 return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"];
125 $c = $this->memcache();
131 $types = $this->keyTypes();
133 foreach ($types as $key => $type) {
136 $pval[] = $this->$key;
138 $c->set($this->cacheKey($this->tableName(), $key, $this->$key), $this);
141 # XXX: should work for both compound and scalar pkeys
142 $pvals = implode(',', $pval);
143 $pkeys = implode(',', $pkey);
144 $c->set($this->cacheKey($this->tableName(), $pkeys, $pvals), $this);
150 $c = $this->memcache();
156 $types = $this->keyTypes();
158 foreach ($types as $key => $type) {
161 $pval[] = $this->$key;
163 $c->delete($this->cacheKey($this->tableName(), $key, $this->$key));
166 # should work for both compound and scalar pkeys
167 # XXX: comma works for now but may not be safe separator for future keys
168 $pvals = implode(',', $pval);
169 $pkeys = implode(',', $pkey);
170 $c->delete($this->cacheKey($this->tableName(), $pkeys, $pvals));
174 function multicache($cls, $kv)
177 $c = Memcached_DataObject::memcache();
181 $pkeys = implode(',', array_keys($kv));
182 $pvals = implode(',', array_values($kv));
183 return $c->get(Memcached_DataObject::cacheKey($cls, $pkeys, $pvals));
187 function getSearchEngine($table)
189 require_once INSTALLDIR.'/lib/search_engines.php';
190 static $search_engine;
191 if (!isset($search_engine)) {
192 if (Event::handle('GetSearchEngine', array($this, $table, &$search_engine))) {
193 if ('mysql' === common_config('db', 'type')) {
194 $type = common_config('search', 'type');
195 if ($type == 'like') {
196 $search_engine = new MySQLLikeSearch($this, $table);
197 } else if ($type == 'fulltext') {
198 $search_engine = new MySQLSearch($this, $table);
200 throw new ServerException('Unknown search type: ' . $type);
203 $search_engine = new PGSearch($this, $table);
207 return $search_engine;
210 static function cachedQuery($cls, $qry, $expiry=3600)
212 $c = Memcached_DataObject::memcache();
218 $key_part = common_keyize($cls).':'.md5($qry);
219 $ckey = common_cache_key($key_part);
220 $stored = $c->get($ckey);
222 return new ArrayWrapper($stored);
228 while ($inst->fetch()) {
229 $cached[] = clone($inst);
232 $c->set($ckey, $cached, MEMCACHE_COMPRESSED, $expiry);
233 return new ArrayWrapper($cached);
236 // We overload so that 'SET NAMES "utf8"' is called for
241 global $_DB_DATAOBJECT;
243 $sum = $this->_getDbDsnMD5();
245 if (!empty($_DB_DATAOBJECT['CONNECTIONS'][$sum]) &&
246 !PEAR::isError($_DB_DATAOBJECT['CONNECTIONS'][$sum])) {
252 $result = parent::_connect();
254 if ($result && !$exists) {
255 $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
256 if (common_config('db', 'type') == 'mysql' &&
257 common_config('db', 'utf8')) {
258 $conn = $DB->connection;
260 if ($DB instanceof DB_mysqli) {
261 mysqli_set_charset($conn, 'utf8');
262 } else if ($DB instanceof DB_mysql) {
263 mysql_set_charset('utf8', $conn);
272 // XXX: largely cadged from DB_DataObject
274 function _getDbDsnMD5()
276 if ($this->_database_dsn_md5) {
277 return $this->_database_dsn_md5;
280 $dsn = $this->_getDbDsn();
282 if (is_string($dsn)) {
285 /// support array based dsn's
286 $sum = md5(serialize($dsn));
294 global $_DB_DATAOBJECT;
296 if (empty($_DB_DATAOBJECT['CONFIG'])) {
297 DB_DataObject::_loadConfig();
300 $options = &$_DB_DATAOBJECT['CONFIG'];
302 // if the databse dsn dis defined in the object..
304 $dsn = isset($this->_database_dsn) ? $this->_database_dsn : null;
308 if (!$this->_database) {
309 $this->_database = isset($options["table_{$this->__table}"]) ? $options["table_{$this->__table}"] : null;
312 if ($this->_database && !empty($options["database_{$this->_database}"])) {
313 $dsn = $options["database_{$this->_database}"];
314 } else if (!empty($options['database'])) {
315 $dsn = $options['database'];
320 throw new Exception("No database name / dsn found anywhere");