db2.class.php
class="language-php"><?phprequire_once("db2.conf.php");
class db2{
public static $link = NULL;
public static function db(){
if(is_null(self::$link)){
self::$link = new mysqli(HOST, USER, PASSWORD, DATABASE, PORT);
}
return new self;
}
public function query($sql){
mysqli_query(self::$link, "SET NAMES ".CHARSET);
return mysqli_query(self::$link, $sql);
}
}
class mem2{
public static $link = NULL;
public static function mem(){
if(is_null(self::$link)){
self::$link = new Memcached();
self::$link->addServer(MEM_HOST, MEM_PORT);
}
return new self;
}
public function set($key, $value, $time){
self::$link->set($key, $value, $time);
}
public function get($key){
return self::$link->get($key);
}
public function clean($key){
if(empty($key)){
return false;
}
$keyList = self::$link->getAllKeys();
foreach($keyList as $value){
if(strpos("key@".$value, $key)){
self::$link->delete($value);
}
}
}
}
class DB{
public $expire = 0;
public static $go = NULL;
public static function go(){
if(is_null(self::$go)){
self::$go = new self();
}
return self::$go;
}
public function query($sql){
$this->sql = $sql;
return $this;
}
public function cache($expire, $prefix="cache"){
$this->expire = $expire;
$this->prefix = $prefix."@";
return $this;
}
public function execute(){
db2::db()->query($this->sql);
}
public function find(){
$hashName = $this->prefix.sha1(md5(__FUNCTION__).$this->sql);
if($this->expire){
$result = mem2::mem()->get($hashName);
if($result){
return unserialize($result);
}
}
$query = db2::db()->query($this->sql);
if($query == false){
return array();
}
$result = mysqli_fetch_assoc($query);
if($this->expire){
mem2::mem()->set($hashName, serialize($result), $this->expire);
}
mysqli_free_result($query);
return $result;
}
public function findAll(){
$hashName = $this->prefix.sha1(md5(__FUNCTION__).$this->sql);
if($this->expire){
$result = mem2::mem()->get($hashName);
if($result){
return unserialize($result);
}
}
$query = db2::db()->query($this->sql);
if($query == false){
return array();
}
while($row = mysqli_fetch_assoc($query)){
$result[] = $row;
}
if($this->expire){
mem2::mem()->set($hashName, serialize($result), $this->expire);
}
mysqli_free_result($query);
return $result;
}
}
以上是 db2.class.php 的全部内容, 来源链接: utcz.com/z/514458.html