Skip to content

Commit

Permalink
完善即时缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
seika committed Oct 10, 2023
1 parent 3d46243 commit 6c83312
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 400 deletions.
4 changes: 0 additions & 4 deletions _config/db.ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
; 是否调试,配合系统的debug为true时,会打印出整个页面执行的SQL语句
debug = false

; 即时缓存,适应用大量的小查询(重复查询,一般不用开启)
; 注意,此项仅在使用了 memcache 或 redis 缓存时才有效,如果使用文件缓存,速度反而会下降
cache = false

;慢查询记录
slow = false

Expand Down
6 changes: 1 addition & 5 deletions framework/_init_phpok.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,7 @@ public function cache($name='')
{
$ini = $name ? 'cache-'.$name : 'cache';
$config = $this->load_config($ini);
$obj = $this->engine('cache',$config);
if($obj){
$this->db()->cache_conn($obj);
}
return $obj;
return $this->engine('cache',$config);
}

/**
Expand Down
142 changes: 100 additions & 42 deletions framework/engine/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ class cache
protected $debug = false;
protected $time;
//
private $keylist = array();
private $timelist = array();
private $md5list = array();
private $time_use = 0;
private $time_tmp = 0;
private $count = 0;
private $safecode = "<?php die('forbidden'); ?>\n";
private $db;

public function __construct($config)
{
Expand All @@ -31,45 +36,43 @@ public function __construct($config)
$this->timeout = $config['timeout'] ? $config['timeout'] : 1800;
$this->prefix = $config["prefix"] ? $config["prefix"] : "qinggan_";
$this->folder = $config['folder'] ? $config['folder'] : '../_cache/';
ksort($config);
$this->key_id = md5($this->prefix."_".serialize($config));
$this->key_list = array();
if($this->status){
$this->key_list = $this->get($this->key_id);
if(!$this->key_list){
$this->key_list = array();
}
}
$this->time = time();
$this->keylist_load();
}

public function __destruct()
{
$this->save($this->key_id,$this->key_list);
$this->expired();
$this->keylist_save();
}

public function key_list($id,$value='',$is_add=false)
public function key_list($id,$tbl='')
{
if(!$value && isset($this->key_list[$id])){
unset($this->key_list[$id]);
return true;
if(!$id || !$tbl){
return false;
}
$tmp = isset($this->key_list[$id]) ? $this->key_list[$id] : array();
if(!is_array($value)){
$value = array($value);
$list = array();
if(is_string($tbl)){
$tbl = trim($tbl);
if(!$tbl){
return false;
}
$list = explode(",",$tbl);
}
if(is_array($tbl)){
$list = $tbl;
}
$tmp = array_merge($tmp,$value);
$tmp = array_unique($tmp);
$this->key_list[$id] = $tmp;
if(!$list || count($list)<1){
return false;
}
foreach($list as $key=>$value){
$this->keylist[$value][$id] = true;
$this->md5list[$id][$value] = true;
}
$this->timelist[$id] = $this->time;
return true;
}

public function key_all()
{
return $this->key_list;
}

public function status($status='')
{
if(is_bool($status) || is_numeric($status)){
Expand Down Expand Up @@ -129,7 +132,7 @@ public function get($id,$onlycheck=false)
$this->_time();
$ftime = filemtime($this->folder.$id.'.php');
if(($ftime + $this->timeout) < $this->time){
$this->delete($id);
$this->delete($id,false);
return false;
}
$this->_count();
Expand Down Expand Up @@ -165,6 +168,7 @@ public function id($var='')
}
}
if(is_array($var) || is_object($var)){
sort($var);
$var = serialize($var);
}
return md5($this->prefix."_".$var);
Expand All @@ -173,8 +177,17 @@ public function id($var='')
public function delete($id)
{
@unlink($this->folder.$id.'.php');
if($this->key_list && $this->key_list[$id]){
unset($this->key_list[$id]);
if(!$this->md5list || !$this->md5list[$id]){
return true;
}
foreach($this->md5list[$id] as $key=>$value){
if($this->keylist && $this->keylist[$key][$id]){
unset($this->keylist[$key][$id]);
}
}
unset($this->md5list[$id]);
if($this->timelist && $this->timelist[$id]){
unset($this->timelist[$id]);
}
return true;
}
Expand All @@ -192,14 +205,15 @@ public function time()
//根据索引删除
public function delete_index($id)
{
foreach($this->key_list as $key=>$value){
if(!$value || !is_array($value)){
continue;
}
if(in_array($id,$value)){
if(!$this->keylist || !$this->keylist[$id]){
return true;
}
foreach($this->keylist[$id] as $key=>$value){
if($this->md5list && $this->md5list[$key][$id]){
$this->delete($key);
}
}
return true;
}

public function clear()
Expand All @@ -213,21 +227,24 @@ public function clear()
}
}
closedir($handle);
$sql = "TRUNCATE ".tablename('cache');
$GLOBALS['app']->db()->query($sql);
return true;
}

public function expired()
{
$handle = opendir($this->folder);
$array = array();
if(!$this->timelist){
return true;
}
$expire_time = $this->time - $this->timeout;
while(false !== ($myfile = readdir($handle))){
if(is_file($this->folder.$myfile) && filemtime($this->folder.$myfile) < $expire_time){
$id = substr($myfile,0,-4);
$this->delete($id);
foreach($this->timelist as $key=>$value){
if($value < $expire_time){
$this->delete($key);
}
}
closedir($handle);
$sql = "DELETE FROM ".tablename('cache')." WHERE dateline<".$expire_time;
$GLOBALS['app']->db()->query($sql);
return true;
}

Expand Down Expand Up @@ -277,6 +294,47 @@ protected function _count($val=1)
$this->count += $val;
}

protected function keylist_load()
{
if(!$this->status){
return false;
}
$expire_time = $this->time - $this->timeout;
$sql = "SELECT * FROM ".tablename("cache")." WHERE dateline>".$expire_time;
$rslist = $GLOBALS['app']->db()->get_all($sql);
if(!$rslist){
return false;
}
foreach($rslist as $key=>$value){
if(!isset($this->keylist[$value['tbl']])){
$this->keylist[$value['tbl']] = array();
}
if(!isset($this->md5list[$value['code']])){
$this->md5list[$value['code']] = array();
}
$this->keylist[$value['tbl']][$value['code']] = true;
$this->md5list[$value['code']][$value['tbl']] = true;
$this->timelist[$value['code']] = $value['dateline'];
}
return true;
}

}
?>
protected function keylist_save()
{
if(!$this->status){
return false;
}
$sql = "REPLACE INTO ".tablename('cache')."(tbl,code,dateline) VALUES";
foreach($this->keylist as $key=>$value){
foreach($value as $k=>$v){
$time = $this->timelist[$k] ? $this->timelist[$k] : $this->time;
$mylist[] = "('".$key."','".$k."','".$time."')";
}
}
if($mylist){
$sql .= implode(",",$mylist);
$GLOBALS['app']->db()->query($sql);
}
return true;
}
}
Loading

0 comments on commit 6c83312

Please sign in to comment.