X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FMemcached_DataObject.php;h=867b40cf3cc0e05363343e9696a3ba56c53437a8;hb=99db745f9d8746e3170bf779746d855c44f0b951;hp=8d54e1f0f012e9610b5c66c11f2167738f76af63;hpb=b42e044f6e5111fe6cb7d4a7b7431cd9b9b67ccb;p=quix0rs-gnu-social.git diff --git a/classes/Memcached_DataObject.php b/classes/Memcached_DataObject.php index 8d54e1f0f0..867b40cf3c 100644 --- a/classes/Memcached_DataObject.php +++ b/classes/Memcached_DataObject.php @@ -74,7 +74,7 @@ class Memcached_DataObject extends Safe_DataObject return $i; } else { $i = DB_DataObject::factory($cls); - if (empty($i)) { + if (empty($i) || PEAR::isError($i)) { return false; } foreach ($kv as $k => $v) { @@ -128,12 +128,13 @@ class Memcached_DataObject extends Safe_DataObject } static function cacheKey($cls, $k, $v) { - if (is_object($cls) || is_object($k) || is_object($v)) { + if (is_object($cls) || is_object($k) || (is_object($v) && !($v instanceof DB_DataObject_Cast))) { $e = new Exception(); common_log(LOG_ERR, __METHOD__ . ' object in param: ' . str_replace("\n", " ", $e->getTraceAsString())); } - return common_cache_key(strtolower($cls).':'.$k.':'.$v); + $vstr = self::valueString($v); + return common_cache_key(strtolower($cls).':'.$k.':'.$vstr); } static function getcached($cls, $k, $v) { @@ -188,11 +189,11 @@ class Memcached_DataObject extends Safe_DataObject str_replace("\n", " ", $e->getTraceAsString())); return false; } else { - $keys = $this->_allCacheKeys(); + $keys = $this->_allCacheKeys(); - foreach ($keys as $key) { - $c->set($key, $this); - } + foreach ($keys as $key) { + $c->set($key, $this); + } } } @@ -229,11 +230,12 @@ class Memcached_DataObject extends Safe_DataObject if (empty($this->$key)) { continue; } - $ckeys[] = $this->cacheKey($this->tableName(), $key, $this->$key); + $ckeys[] = $this->cacheKey($this->tableName(), $key, self::valueString($this->$key)); } else if ($type == 'K' || $type == 'N') { $pkey[] = $key; - $pval[] = $this->$key; + $pval[] = self::valueString($this->$key); } else { + // Low level exception. No need for i18n as discussed with Brion. throw new Exception("Unknown key type $key => $type for " . $this->tableName()); } } @@ -281,6 +283,7 @@ class Memcached_DataObject extends Safe_DataObject } else if ($type == 'fulltext') { $search_engine = new MySQLSearch($this, $table); } else { + // Low level exception. No need for i18n as discussed with Brion. throw new ServerException('Unknown search type: ' . $type); } } else { @@ -335,7 +338,12 @@ class Memcached_DataObject extends Safe_DataObject } $start = microtime(true); - $result = parent::_query($string); + $result = null; + if (Event::handle('StartDBQuery', array($this, $string, &$result))) { + common_perf_counter('query', $string); + $result = parent::_query($string); + Event::handle('EndDBQuery', array($this, $string, &$result)); + } $delta = microtime(true) - $start; $limit = common_config('db', 'log_slow_queries'); @@ -351,7 +359,7 @@ class Memcached_DataObject extends Safe_DataObject * low-level database function and add a comment to the * query string. This should then be visible in process lists * and slow query logs, to help identify problem areas. - * + * * Also marks whether this was a web GET/POST or which daemon * was running it. * @@ -473,6 +481,10 @@ class Memcached_DataObject extends Safe_DataObject } } } + // Needed to make timestamp values usefully comparable. + if (common_config('db', 'type') == 'mysql') { + parent::_query("set time_zone='+0:00'"); + } } return $result; @@ -526,7 +538,8 @@ class Memcached_DataObject extends Safe_DataObject } if (!$dsn) { - throw new Exception("No database name / dsn found anywhere"); + // TRANS: Exception thrown when database name or Data Source Name could not be found. + throw new Exception(_("No database name or DSN found anywhere.")); } return $dsn; @@ -570,9 +583,13 @@ class Memcached_DataObject extends Safe_DataObject function raiseError($message, $type = null, $behaviour = null) { $id = get_class($this); - if ($this->id) { + if (!empty($this->id)) { $id .= ':' . $this->id; } + if ($message instanceof PEAR_Error) { + $message = $message->getMessage(); + } + // Low level exception. No need for i18n as discussed with Brion. throw new ServerException("[$id] DB_DataObject error [$type]: $message"); } @@ -589,7 +606,7 @@ class Memcached_DataObject extends Safe_DataObject return $c->get($cacheKey); } - static function cacheSet($keyPart, $value) + static function cacheSet($keyPart, $value, $flag=null, $expiry=null) { $c = self::memcache(); @@ -599,7 +616,33 @@ class Memcached_DataObject extends Safe_DataObject $cacheKey = common_cache_key($keyPart); - return $c->set($cacheKey, $value); + return $c->set($cacheKey, $value, $flag, $expiry); } -} + static function valueString($v) + { + $vstr = null; + if (is_object($v) && $v instanceof DB_DataObject_Cast) { + switch ($v->type) { + case 'date': + $vstr = $v->year . '-' . $v->month . '-' . $v->day; + break; + case 'blob': + case 'string': + case 'sql': + case 'datetime': + case 'time': + // Low level exception. No need for i18n as discussed with Brion. + throw new ServerException("Unhandled DB_DataObject_Cast type passed as cacheKey value: '$v->type'"); + break; + default: + // Low level exception. No need for i18n as discussed with Brion. + throw new ServerException("Unknown DB_DataObject_Cast type passed as cacheKey value: '$v->type'"); + break; + } + } else { + $vstr = strval($v); + } + return $vstr; + } +}