diff --git a/classes/class_cache_redis.php b/classes/class_cache_redis.php index 0b24c61c..0c42d304 100644 --- a/classes/class_cache_redis.php +++ b/classes/class_cache_redis.php @@ -205,6 +205,7 @@ class RedisCache { // Wrapper for Memcache::set, with the zlib option removed and default duration of 1 hour function cache_value($Key, $Value, $Duration = 3600){ + $Value = $this->serialize($Value); // $this->set($Key,$Value, 0, $Duration); $this->redis->set($Key, $Value, $Duration); $this->cacheWriteTimes++; @@ -273,6 +274,7 @@ class RedisCache { }*/ $Return = $this->redis->get($Key); + $Return = ! is_null($Return) ? $this->unserialize($Return) : null; $this->cacheReadTimes++; $this->keyHits['read'][$Key] = !$this->keyHits['read'][$Key] ? 1 : $this->keyHits['read'][$Key]+1; return $Return; @@ -301,4 +303,26 @@ class RedisCache { function getKeyHits ($type='read') { return (array)$this->keyHits[$type]; } + + /** + * Serialize the value. + * + * @param mixed $value + * @return mixed + */ + protected function serialize($value) + { + return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value) ? $value : serialize($value); + } + + /** + * Unserialize the value. + * + * @param mixed $value + * @return mixed + */ + protected function unserialize($value) + { + return is_numeric($value) ? $value : unserialize($value); + } } diff --git a/include/bittorrent.php b/include/bittorrent.php index 110e4404..a8256327 100644 --- a/include/bittorrent.php +++ b/include/bittorrent.php @@ -12,9 +12,10 @@ $rootpath .= "/"; require $rootpath . 'include/config.php'; require $rootpath . 'include/functions.php'; -require $rootpath . 'classes/interface_db.php'; -require $rootpath . 'classes/class_db_mysqli.php'; -require $rootpath . 'classes/class_db.php'; -require $rootpath . 'include/functions_db.php'; +require $rootpath . 'include/database/interface_db.php'; +require $rootpath . 'include/database/class_db_mysqli.php'; +require $rootpath . 'include/database/class_db.php'; +require $rootpath . 'include/database/helpers.php'; +require $rootpath . 'include/database/class_exception.php'; require $rootpath . 'include/core.php'; diff --git a/classes/class_db.php b/include/database/class_db.php similarity index 84% rename from classes/class_db.php rename to include/database/class_db.php index 84f35a43..3e1a2215 100644 --- a/classes/class_db.php +++ b/include/database/class_db.php @@ -44,7 +44,12 @@ class DB public function query(string $sql) { - return $this->driver->query($sql); + try { + return $this->driver->query($sql); + } catch (\Exception $e) { + throw new \DatabaseException($sql, $e->getMessage()); + } + } public function error() @@ -77,6 +82,11 @@ class DB return $this->driver->fetchRow($result); } + public function fetchArray($result, $type = null) + { + return $this->driver->fetchArray($result, $type); + } + public function affectedRows() { return $this->driver->affectedRows(); diff --git a/classes/class_db_mysqli.php b/include/database/class_db_mysqli.php similarity index 88% rename from classes/class_db_mysqli.php rename to include/database/class_db_mysqli.php index 754dfbe7..7708f7b6 100644 --- a/classes/class_db_mysqli.php +++ b/include/database/class_db_mysqli.php @@ -53,6 +53,14 @@ class DBMysqli implements DBInterface return $mysqliResult->fetch_row(); } + public function fetchArray($mysqliResult, $type): array|null + { + if (is_null($type)) { + $type = MYSQLI_BOTH; + } + return $mysqliResult->fetch_array($type); + } + public function affectedRows(): int { return $this->mysqli->affected_rows; diff --git a/include/database/class_exception.php b/include/database/class_exception.php new file mode 100644 index 00000000..b66eb8e6 --- /dev/null +++ b/include/database/class_exception.php @@ -0,0 +1,11 @@ +numRows($result); } -function mysql_fetch_array($result) +function mysql_fetch_array($result, $type = null) { - return DB::getInstance()->fetchAssoc($result); + return DB::getInstance()->fetchArray($result, $type); } function mysql_fetch_assoc($result) diff --git a/classes/interface_db.php b/include/database/interface_db.php similarity index 89% rename from classes/interface_db.php rename to include/database/interface_db.php index 0ac07825..1aee6cba 100644 --- a/classes/interface_db.php +++ b/include/database/interface_db.php @@ -18,6 +18,8 @@ interface DBInterface public function fetchRow($result): array|null; + public function fetchArray($result, $type): array|null; + public function affectedRows(): int; public function escapeString(string $string): string;