日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php缓存类,PHP缓存类

發(fā)布時(shí)間:2023/12/1 php 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php缓存类,PHP缓存类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

// +----------------------------------------------------------------------

// |緩存類

// +----------------------------------------------------------------------

// | Author: justmepzy(justmepzy@gmail.com)

// +----------------------------------------------------------------------

class Cache{

//提示信息

public $tip_message;

//緩存目錄

protected $cache_dir;

//緩存文件名

private $cache_file_name;

//緩存文件后綴

private $cache_file_suffix;

public function __construct($dir,$cache_file_suffix ='.php'){

$this->cache_dir = isset($dir)?$dir:dirname(__FILE__).DIRECTORY_SEPARATOR.'default_cache_data';

$this->cache_file_suffix =$cache_file_suffix;

if(!$this->dir_isvalid($this->cache_dir)){

die($this->tip_message);//創(chuàng)建目錄失敗

}

}

// +----------------------------------------------------------------------

// |添加一個(gè)值,如果已經(jīng)存在,則返回false,寫入文件失敗返回false

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function add($cache_key,$cache_value,$life_time=1800){

if(file_exists($this->get_cache_file_name($cache_key))){

$this->tip_message ='緩存數(shù)據(jù)已存在.';

return false;

}

$cache_data['data'] =$cache_value;

$cache_data['life_time'] =$life_time;

//以JSON格式寫入文件

if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){

return true;

}else{

$this->tip_message ='寫入緩存失敗.';

return false;

}

}

// +----------------------------------------------------------------------

// |添加一個(gè)值,如果已經(jīng)存在,則覆寫

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function set($cache_key,$cache_value,$life_time=1800){

$cache_data['data'] =$cache_value;

$cache_data['life_time'] =$life_time;

if(file_put_contents($this->get_cache_file_name($cache_key), json_encode($cache_data))){

return true;

}else{

$this->tip_message ='寫入緩存失敗.';

return false;

}

}

// +----------------------------------------------------------------------

// |獲取一個(gè)key值

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function get($cache_key){

if(!file_exists($this->get_cache_file_name($cache_key))){

return false;

}

$data =$this->object_to_array(json_decode(file_get_contents($this->get_cache_file_name($cache_key))));

if($this->check_isvalid($data['life_time'])){

unset($data['life_time']);

return $data['data'];

}else{

unlink($this->cache_file_name);

$this->tip_message ='數(shù)據(jù)已過期.';

return false;

}

}

// +----------------------------------------------------------------------

// |刪除一個(gè)key值

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function delete($cache_key){

if(file_exists($this->get_cache_file_name($cache_key))){

if(unlink($this->get_cache_file_name($cache_key)))

return true;

else

return false;

}else{

$this->tip_message ='文件不存在.';

return true;

}

}

// +----------------------------------------------------------------------

// |清除所有緩存文件

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function flush(){

$this->delete_file($this->cache_dir);

}

// +----------------------------------------------------------------------

// |自動清除過期文件

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

public function auto_delete_expired_file(){

$this->delete_file($this->cache_dir,false);

}

// +----------------------------------------------------------------------

// |檢查目錄是否存在,不存在則創(chuàng)建

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function dir_isvalid($dir){

if (is_dir($dir))

return true;

try {

mkdir($dir,0777);

}catch (Exception$e) {

$this->tip_message ='所設(shè)定緩存目錄不存在并且創(chuàng)建失敗!請檢查目錄權(quán)限!';

return false;

}

return true;

}

// +----------------------------------------------------------------------

// |檢查有效時(shí)間

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function check_isvalid($expired_time = 0) {

//if(!file_exists($this->cache_file_name)) return false;

if (!(@$mtime =filemtime($this->cache_file_name)))return false;

if (time() -$mtime >$expired_time)return false;

return true;

}

// +----------------------------------------------------------------------

// |獲得緩存文件名

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

private function get_cache_file_name($key){

$this->cache_file_name =$this->cache_dir.DIRECTORY_SEPARATOR.md5($key).$this->cache_file_suffix;

return $this->cache_file_name;

}

// +----------------------------------------------------------------------

// |object對象轉(zhuǎn)換為數(shù)組

// +----------------------------------------------------------------------

// |

// +----------------------------------------------------------------------

protected function object_to_array($obj){

$_arr =is_object($obj) ? get_object_vars($obj) :$obj;

foreach ($_arr as $key =>$val) {

$val = (is_array($val) ||is_object($val)) ? object_to_array($val) :$val;

$arr[$key] =$val;

}

return $arr;

}

// +----------------------------------------------------------------------

// |刪除目錄下的所有文件

// +----------------------------------------------------------------------

// | $mode true刪除所有 false刪除過期

// +----------------------------------------------------------------------

protected function delete_file($dir,$mode=true) {

$dh=opendir($dir);

while ($file=readdir($dh)) {

if($file!="." &&$file!="..") {

$fullpath=$dir."/".$file;

if(!is_dir($fullpath)) {

if($mode){

unlink($fullpath);

}else{

$this->cache_file_name =$fullpath;

if(!$this->get_isvalid_by_path($fullpath)) unlink($fullpath);

}

}else {

delete_file($fullpath,$mode);

}

}

}

closedir($dh);

}

private function get_isvalid_by_path($path){

$data =$this->object_to_array(json_decode(file_get_contents($path)));

return $this->check_isvalid($data['life_time']);

}

}

?>

總結(jié)

以上是生活随笔為你收集整理的php缓存类,PHP缓存类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。