add database directory

This commit is contained in:
xiaomlove
2020-12-28 02:14:41 +08:00
parent da1ee77550
commit 77ebc7caa4
7 changed files with 63 additions and 7 deletions

View File

@@ -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);
}
}

View File

@@ -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';

View File

@@ -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();

View File

@@ -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;

View File

@@ -0,0 +1,11 @@
<?php
class DatabaseException extends \Exception
{
public function __construct($query, $message)
{
parent::__construct("$message [$query]");
}
}

View File

@@ -30,9 +30,9 @@ function mysql_num_rows($result)
return DB::getInstance()->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)

View File

@@ -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;