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 6c83312 commit bf98923
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
48 changes: 32 additions & 16 deletions framework/engine/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class cache
private $time_use = 0;
private $time_tmp = 0;
private $count = 0;
private $safecode = "<?php die('forbidden'); ?>\n";
private $db;
private $safecode = "<?php die('forbidden'); ?>";

public function __construct($config)
{
Expand Down Expand Up @@ -227,8 +226,6 @@ public function clear()
}
}
closedir($handle);
$sql = "TRUNCATE ".tablename('cache');
$GLOBALS['app']->db()->query($sql);
return true;
}

Expand All @@ -243,8 +240,6 @@ public function expired()
$this->delete($key);
}
}
$sql = "DELETE FROM ".tablename('cache')." WHERE dateline<".$expire_time;
$GLOBALS['app']->db()->query($sql);
return true;
}

Expand Down Expand Up @@ -299,12 +294,12 @@ 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){
$this->keyfile = $this->prefix."keylist.php";
$file = $this->folder.$this->keyfile;
if(!file_exists($file)){
return false;
}
$rslist = $this->keylist_data($file);
foreach($rslist as $key=>$value){
if(!isset($this->keylist[$value['tbl']])){
$this->keylist[$value['tbl']] = array();
Expand All @@ -324,17 +319,38 @@ protected function keylist_save()
if(!$this->status){
return false;
}
$sql = "REPLACE INTO ".tablename('cache')."(tbl,code,dateline) VALUES";
$list = array();
foreach($this->keylist as $key=>$value){
foreach($value as $k=>$v){
$time = $this->timelist[$k] ? $this->timelist[$k] : $this->time;
$mylist[] = "('".$key."','".$k."','".$time."')";
$list[] = $k.'[|]'.$time.'[|]'.$key;
}
}
if($mylist){
$sql .= implode(",",$mylist);
$GLOBALS['app']->db()->query($sql);
}
$content = implode("\n",$list);
$file = $this->folder.$this->keyfile;
file_put_contents($file,$this->safecode."\n".$content);
return true;
}

protected function keylist_data($file)
{
$list = file($file,FILE_SKIP_EMPTY_LINES);
if(!$list){
return false;
}
unset($list[0]);
if(!$list){
return false;
}
$rslist = array();
foreach($list as $key=>$value){
$value = trim($value);
$tmp = explode("[|]",$value);
if(!$tmp[0] || !$tmp[1] || !$tmp[2]){
continue;
}
$rslist[] = array('code'=>$tmp[0],'dateline'=>$tmp[1],'tbl'=>$tmp[2]);
}
return $rslist;
}
}
54 changes: 51 additions & 3 deletions framework/engine/cache/redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,18 @@ public function delete($id)
if($this->conn){
$this->conn->delete($id);
}
if($this->key_list && $this->key_list){
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 @@ -165,4 +173,44 @@ public function expired()
{
return true;
}

protected function keylist_load()
{
if(!$this->status){
return false;
}
$this->keyfile = $this->prefix."keylist";
$rslist = $this->get($this->keyfile);
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 || !$this->keylist){
return false;
}
$list = array();
foreach($this->keylist as $key=>$value){
foreach($value as $k=>$v){
$time = $this->timelist[$k] ? $this->timelist[$k] : $this->time;
$list[] = array('code'=>$k,'dateline'=>$time,'tbl'=>$key);
}
}
$this->save($this->keyfile,$list);
return true;
}
}
15 changes: 15 additions & 0 deletions framework/engine/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ public function debug($sql='',$time=0)
}
return true;
}
$html = '<table class="table table-bordered">';
$html .= '<tr>';
$html .= '<th>Count</th>';
$html .= '<th>Time</th>';
$html .= '<th>SQL</th>';
$html .= '</tr>';
foreach($this->_sqlist as $key=>$value){
$html .= '<tr>';
$html .= '<td>'.$value['count'].'</td>';
$html .= '<td>'.$value['time'].'</td>';
$html .= '<td class="text-start">'.$value['sql'].'</td>';
$html .= '</tr>';
}
$html.= '</table>';
return $html;
}


Expand Down
4 changes: 1 addition & 3 deletions framework/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,12 @@ function debug_time()
$sql_db_time = $app->db()->sql_time();
$cache_count = $app->cache()->count();
$cache_time = $app->cache()->time();
$string = '运行 {total} 秒,内存使用 {mem_total},数据库执行 {sql_count} 次,';
$string.= '用时 {sql_time} 秒,缓存执行 {cache_count} 次,用时 {cache_time} 秒';
$array = array('total'=>$time,'mem_total'=>$memory);
$array['sql_count']= $app->db()->sql_count();
$array['sql_time'] = $app->db()->sql_time();
$array['cache_count'] = $app->cache()->count();
$array['cache_time'] = $app->cache()->time();
$string = P_Lang($string,$array);
$string = P_Lang('运行 {total} 秒,内存使用 {mem_total},数据库执行 {sql_count} 次,用时 {sql_time} 秒,缓存执行 {cache_count} 次,用时 {cache_time} 秒',$array);
return $string;
}

Expand Down

0 comments on commit bf98923

Please sign in to comment.