mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-03 14:10:57 +08:00
add database directory
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
class DB
|
||||
{
|
||||
private DBInterface $driver;
|
||||
|
||||
private static $instance;
|
||||
|
||||
private static array $queries = [];
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private function __clone()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function setDriver(DBInterface $driver)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance) {
|
||||
return self::$instance;
|
||||
}
|
||||
$instance = new self;
|
||||
$driver = new DBMysqli();
|
||||
$instance->setDriver($driver);
|
||||
return self::$instance = $instance;
|
||||
}
|
||||
|
||||
|
||||
public function connect($host, $username, $password, $database, $port)
|
||||
{
|
||||
return $this->driver->connect($host, $username, $password, $database, $port);
|
||||
}
|
||||
|
||||
public function query(string $sql)
|
||||
{
|
||||
return $this->driver->query($sql);
|
||||
}
|
||||
|
||||
public function error()
|
||||
{
|
||||
return $this->driver->error();
|
||||
}
|
||||
|
||||
public function errno()
|
||||
{
|
||||
return $this->driver->errno();
|
||||
}
|
||||
|
||||
public function numRows($result)
|
||||
{
|
||||
return $this->driver->numRows($result);
|
||||
}
|
||||
|
||||
public function select_db($database)
|
||||
{
|
||||
return $this->driver->selectDb($database);
|
||||
}
|
||||
|
||||
public function fetchAssoc($result)
|
||||
{
|
||||
return $this->driver->fetchAssoc($result);
|
||||
}
|
||||
|
||||
public function fetchRow($result)
|
||||
{
|
||||
return $this->driver->fetchRow($result);
|
||||
}
|
||||
|
||||
public function affectedRows()
|
||||
{
|
||||
return $this->driver->affectedRows();
|
||||
}
|
||||
|
||||
public function escapeString(string $string)
|
||||
{
|
||||
return $this->driver->escapeString($string);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
class DBMysqli implements DBInterface
|
||||
{
|
||||
private mysqli $mysqli;
|
||||
|
||||
public function connect($host, $username, $password, $database, $port)
|
||||
{
|
||||
$mysqli = new mysqli($host, $username, $password, $database, $port);
|
||||
/* check connection */
|
||||
if (mysqli_connect_errno()) {
|
||||
throw new \RuntimeException(mysqli_connect_error());
|
||||
}
|
||||
/* activate reporting */
|
||||
$driver = new mysqli_driver();
|
||||
$driver->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX;
|
||||
|
||||
return $this->mysqli = $mysqli;
|
||||
}
|
||||
|
||||
public function query(string $sql)
|
||||
{
|
||||
return $this->mysqli->query($sql);
|
||||
}
|
||||
|
||||
public function error(): string
|
||||
{
|
||||
return $this->mysqli->error;
|
||||
}
|
||||
|
||||
public function errno(): int
|
||||
{
|
||||
return $this->mysqli->errno;
|
||||
}
|
||||
|
||||
public function numRows($mysqliResult): int
|
||||
{
|
||||
return $mysqliResult->num_rows;
|
||||
}
|
||||
|
||||
public function selectDb($database)
|
||||
{
|
||||
return $this->mysqli->select_db($database);
|
||||
}
|
||||
|
||||
public function fetchAssoc($mysqliResult): array|null
|
||||
{
|
||||
return $mysqliResult->fetch_assoc();
|
||||
}
|
||||
|
||||
public function fetchRow($mysqliResult): array|null
|
||||
{
|
||||
return $mysqliResult->fetch_row();
|
||||
}
|
||||
|
||||
public function affectedRows(): int
|
||||
{
|
||||
return $this->mysqli->affected_rows;
|
||||
}
|
||||
|
||||
public function escapeString(string $string): string
|
||||
{
|
||||
return $this->mysqli->real_escape_string($string);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
interface DBInterface
|
||||
{
|
||||
public function connect($host, $username, $password, $database, $port);
|
||||
|
||||
public function query(string $sql);
|
||||
|
||||
public function error(): string;
|
||||
|
||||
public function errno(): int;
|
||||
|
||||
public function numRows($result): int;
|
||||
|
||||
public function selectDb($database);
|
||||
|
||||
public function fetchAssoc($result): array|null;
|
||||
|
||||
public function fetchRow($result): array|null;
|
||||
|
||||
public function affectedRows(): int;
|
||||
|
||||
public function escapeString(string $string): string;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user