php简单缓存学习
<?php
//緩存類
class cache
{
??????var $cache_life_time=900;//緩存生命時間
??????var $cache_dir=’./tmp/’;//緩存目錄
??????var $cache_file=’php_cache.php’;//緩存文件名
??????var $is_caching=false;//是否緩存
???????function cache()
??????{
????????????$this->file=$this->cache_dir.$this->file;//緩存文件
?????????????if(file_exists($this->file)??&& (?fileatime($this->file)+$this->cache_life_time?)< time() )//如果緩存文件存在,并且緩存時間未過期
????????????{
????????????????????echo?file_get_contents($this->file);//輸出緩存
????????????????????exit();//退出
????????????}
????????????else
???????????{
??????????????????$this->is_caching=true;
???????????}
??????}
?????function close()
????{
??????????$cache_data=ob_get_clean();//獲取緩存中的內容并清空緩存
???????????echo $cache_data;//輸入緩存
???????????$fp=fopen($this->file,’w');//打開文件
???????????fwrite($fp,$cache_data);//寫入緩存
???????????fclose();//關閉文件
????}
}
//使用事例
$cache= new cache();
echo ‘緩存一下’;
$cache->close();
?>
總結
- 上一篇: css兼容性
- 下一篇: PHP中的魔术方法总结 :__const