本文目錄一覽:
php偽靜態情況下實現的文件緩存實現代碼
PHP靜態文件生成方法:
ob_start(); //開啟緩存
require_once(‘./templates/moban.php’); //導入模板文件(頁面)
file_put_contents(‘index.html’,ob_get_contents()); //生成靜態文件index.html
php文件緩存類匯總
本文實例講述了php的文件緩存類。分享給大家供大家參考。具體分析如下:
緩存類是我們開發應用中會常用使用到的功能,下面就來給大家整理幾個php文件緩存類了,各個文件緩存類寫法不同,但在性能上會有區別,有興趣測試的朋友可測試一下這些緩存類。
例1
複製代碼
代碼如下:?php
$fzz
=
new
fzz_cache;
$fzz-kk
=
$_SERVER;
//寫入緩存
//$fzz-set(“kk”,$_SERVER,10000);
//此方法不與類屬性想衝突,可以用任意緩存名;
print_r($fzz-kk);
//讀取緩存
//print_r($fzz-get(“kk”));
//unset($fzz-kk);
//刪除緩存
//$fzz-_unset(“kk”);
var_dump(isset($fzz-kk));
//判斷緩存是否存在
//$fzz-_isset(“kk”);
//$fzz-clear();
//清理過期緩存
//$fzz-clear_all();
//清理所有緩存文件
class
fzz_cache{
public
$limit_time
=
20000;
//緩存過期時間
public
$cache_dir
=
“data”;
//緩存文件保存目錄
//寫入緩存
function
__set($key
,
$val){
$this-_set($key
,$val);
}
//第三個參數為過期時間
function
_set($key
,$val,$limit_time=null){
$limit_time
=
$limit_time
?
$limit_time
:
$this-limit_time;
$file
=
$this-cache_dir.”/”.$key.”.cache”;
$val
=
serialize($val);
@file_put_contents($file,$val)
or
$this-error(__line__,”fail
to
write
in
file”);
@chmod($file,0777);
@touch($file,time()+$limit_time)
or
$this-error(__line__,”fail
to
change
time”);
}
//讀取緩存
function
__get($key){
return
$this-_get($key);
}
function
_get($key){
$file
=
$this-cache_dir.”/”.$key.”.cache”;
if
(@filemtime($file)=time()){
return
unserialize(file_get_contents($file));
}else{
@unlink($file)
or
$this-error(__line__,”fail
to
unlink”);
return
false;
}
}
//刪除緩存文件
function
__unset($key){
return
$this-_unset($key);
}
function
_unset($key){
if
(@unlink($this-cache_dir.”/”.$key.”.cache”)){
return
true;
}else{
return
false;
}
}
//檢查緩存是否存在,過期則認為不存在
function
__isset($key){
return
$this-_isset($key);
}
function
_isset($key){
$file
=
$this-cache_dir.”/”.$key.”.cache”;
if
(@filemtime($file)=time()){
return
true;
}else{
@unlink($file)
;
return
false;
}
}
//清除過期緩存文件
function
clear(){
$files
=
scandir($this-cache_dir);
foreach
($files
as
$val){
if
(filemtime($this-cache_dir.”/”.$val)time()){
@unlink($this-cache_dir.”/”.$val);
}
}
}
//清除所有緩存文件
function
clear_all(){
$files
=
scandir($this-cache_dir);
foreach
($files
as
$val){
@unlink($this-cache_dir.”/”.$val);
}
}
function
error($msg,$debug
=
false)
{
$err
=
new
Exception($msg);
$str
=
“pre
span
style=’color:red’error:/span
“.print_r($err-getTrace(),1).”
/pre”;
if($debug
==
true)
{
file_put_contents(date(‘Y-m-d
H_i_s’).”.log”,$str);
return
$str;
}else{
die($str);
}
}
}
?
php 中如何使用緩存,使用哪種緩存機制最好;
php的緩存三種.有文件緩存,數據庫緩存,memcache緩存;
memcache緩存要求對服務器支持,而且它的緩存是由期限的,一般是30天。這種緩存的效率是最高的。讀存取的速度最快。
數據庫緩存
和
文件緩存比較簡單。適用小的項目。和php新手
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/198512.html