1 Star 0 Fork 5

模板119免费下源码/xiunoPHP

forked from 轩辕/xiunoPHP 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cache_mysql.class.php 3.07 KB
一键复制 编辑 原始数据 按行查看 历史
eue.cc 提交于 2021-12-26 09:43 . initial commit
<?php
/*
# 持久的 key value 数据存储
DROP TABLE IF EXISTS bbs_kv;
CREATE TABLE bbs_kv (
k char(32) NOT NULL default '',
v mediumtext NOT NULL,
expiry int(11) unsigned NOT NULL default '0', # 过期时间
PRIMARY KEY(k)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
*/
class cache_mysql {
public $conf = array();
public $db = NULL;
public $link = NULL;
public $table = 'cache';
public $cachepre = '';
public $errno = 0;
public $errstr = '';
public function __construct($dbconf = array()) {
// 可以复用全局的 $db
if(is_object($dbconf['db'])) {
$this->db = $dbconf['db']; // 可以直接传 $db 进来
} else {
$this->conf = $dbconf;
$this->db = db_new($dbconf);
}
$this->cachepre = isset($dbconf['cachepre']) ? $dbconf['cachepre'] : 'pre_';
}
public function connect() {
return db_connect($this->db);
}
public function set($k, $v, $life = 0) {
$time = time();
$expiry = $life ? $time + $life : 0;
$arr= array(
'k'=>$k,
'v'=>xn_json_encode($v),
'expiry'=>$expiry,
);
$r = db_replace($this->table, $arr, $this->db);
if($r === FALSE) {
$this->errno = $this->db->errno;
$this->errstr = $this->db->errstr;
return FALSE;
}
return $r !== FALSE;
}
public function get($k) {
$time = time();
$arr = db_find_one($this->table, array('k'=>$k), array(), array(), $this->db);
// 如果表不存在,则建立表 pre_cache
if($arr === FALSE) {
$this->errno = $this->db->errno;
$this->errstr = $this->db->errstr;
return FALSE;
}
if(!$arr) return NULL;
if($arr['expiry'] && $time > $arr['expiry']) {
db_delete($this->table, array('k'=>$k), $this->db);
return NULL;
}
return xn_json_decode($arr['v'], 1);
}
public function delete($k) {
$r = db_delete($this->table, array('k'=>$k), $this->db);
if($r === FALSE) {
$this->errno = $this->db->errno;
$this->errstr = $this->db->errstr;
return FALSE;
}
return empty($r) ? FALSE : TRUE;
}
public function truncate() {
$r = db_truncate($this->table, $this->db);
if($r === FALSE) {
$this->errno = $this->db->errno;
$this->errstr = $this->db->errstr;
return FALSE;
}
return TRUE;
}
public function error($errno, $errstr) {
$this->errno = $errno;
$this->errstr = $errstr;
}
public function __destruct() {
}
}
?>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mb119_com/xiuno-php.git
git@gitee.com:mb119_com/xiuno-php.git
mb119_com
xiuno-php
xiunoPHP
master

搜索帮助