欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一個簡單至極的PHP緩存類代碼

 更新時間:2015年10月23日 15:43:35   投稿:lijiao  
這篇文章和大家分享了一個簡單至極的PHP緩存類代碼,緩存的應用對于php項目的開發(fā)來說尤為重要,需要的朋友可以參考下

網(wǎng)上關于 PHP 緩存類的資料很多,不過這個類應該是我見過功能滿足需求,但又無比簡潔的一個。廢話不多說,直接看代碼吧!
使用說明:
1、實例化
$cache = new Cache();
2、設置緩存時間和緩存目錄
$cache = new Cache(60, '/any_other_path/');
第一個參數(shù)是緩存秒數(shù),第二個參數(shù)是緩存路徑,根據(jù)需要配置。
默認情況下,緩存時間是 3600 秒,緩存目錄是 cache/
3、讀取緩存
$value = $cache->get('data_key');
4、寫入緩存
$value = $cache->put('data_key', 'data_value');
完整實例:

$cache = new Cache(); 
 
//從緩存從讀取鍵值 $key 的數(shù)據(jù) 
$values = $cache->get($key); 
 
//如果沒有緩存數(shù)據(jù) 
if ($values == false) { 
//insert code here... 
//寫入鍵值 $key 的數(shù)據(jù) 
$cache->put($key, $values); 
} else { 
//insert code here... 
} 

Cache.class.php

<?php 
class Cache { 
private $cache_path;//path for the cache 
private $cache_expire;//seconds that the cache expires 
 
//cache constructor, optional expiring time and cache path 
public function Cache($exp_time=3600,$path="cache/"){ 
$this->cache_expire=$exp_time; 
$this->cache_path=$path; 
} 
 
//returns the filename for the cache 
private function fileName($key){ 
return $this->cache_path.md5($key); 
} 
 
//creates new cache files with the given data, $key== name of the cache, data the info/values to store 
public function put($key, $data){ 
$values = serialize($data); 
$filename = $this->fileName($key); 
$file = fopen($filename, 'w'); 
if ($file){//able to create the file 
fwrite($file, $values); 
fclose($file); 
} 
else return false; 
} 
 
//returns cache for the given key 
public function get($key){ 
$filename = $this->fileName($key); 
if (!file_exists($filename) || !is_readable($filename)){//can't read the cache 
return false; 
} 
if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired 
$file = fopen($filename, "r");// read data file 
if ($file){//able to open the file 
$data = fread($file, filesize($filename)); 
fclose($file); 
return unserialize($data);//return the values 
} 
else return false; 
} 
else return false;//was expired you need to create new 
} 
} 
?> 

相信大家一定會喜歡這個簡潔的php緩存類代碼,希望對大家的學習有所幫助。

相關文章

  • php 方便水印和縮略圖的圖形類

    php 方便水印和縮略圖的圖形類

    這是個方便做水印和縮略圖的類,簡化一些操作,按照一些CSS的習慣寫參數(shù)
    2009-05-05
  • 簡單談談PHP中的Reload操作

    簡單談談PHP中的Reload操作

    通常修改了 PHP 的配置后,為了讓修改生效會執(zhí)行 reload,而不是 restart,但最近在使用Reload操作的時候發(fā)現(xiàn)了502錯誤,想著還是要重新思考這個問題。所以這篇文章主要給大家介紹了關于PHP中Reload操作的內(nèi)容,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • PHP爆絕對路徑方法收集整理

    PHP爆絕對路徑方法收集整理

    PHP爆絕對路徑方法收集整理,php開發(fā)人員一定要注意的情況,注意安全防范
    2012-09-09
  • PHP正確配置mysql(apache環(huán)境)

    PHP正確配置mysql(apache環(huán)境)

    以前說實話也配置過不少PHP網(wǎng)站,不過今天在弄一個CMS時還是出現(xiàn)了一個不可饒恕的錯誤,無法連接mysql
    2011-08-08
  • php實現(xiàn)字符串首字母轉換成大寫的方法

    php實現(xiàn)字符串首字母轉換成大寫的方法

    這篇文章主要介紹了php實現(xiàn)字符串首字母轉換成大寫的方法,涉及php中ucfirst及ucwords函數(shù)的使用技巧,需要的朋友可以參考下
    2015-03-03
  • php5.4以下版本json不支持不轉義內(nèi)容中文的解決方法

    php5.4以下版本json不支持不轉義內(nèi)容中文的解決方法

    這篇文章主要介紹了php5.4以下版本json不支持不轉義內(nèi)容中文的解決方法,通過一個自定義php方法實現(xiàn)模擬joson中文不轉義,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • php中http_build_query 的一個問題

    php中http_build_query 的一個問題

    http_build_query 遠程攻擊者可以利用漏洞獲得敏感內(nèi)存信息。請大家謹慎使用
    2012-03-03
  • PHP之十六個魔術方法詳細介紹

    PHP之十六個魔術方法詳細介紹

    PHP中把以兩個下劃線__開頭的方法稱為魔術方法(Magic methods),這些方法在PHP中充當了舉足輕重的作用。這里進行詳細介紹,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • PHP實現(xiàn)仿百度文庫,豆丁在線文檔效果(word,excel,ppt轉flash)

    PHP實現(xiàn)仿百度文庫,豆丁在線文檔效果(word,excel,ppt轉flash)

    這篇文章主要介紹了PHP實現(xiàn)仿百度文庫,豆丁在線文檔效果,可實現(xiàn)word,excel,ppt轉flash顯示的功能,結合實例形式分析了常見的解決方案與CentOS環(huán)境下的實現(xiàn)技巧,需要的朋友可以參考下
    2016-03-03
  • php微信高級接口群發(fā) 多客服

    php微信高級接口群發(fā) 多客服

    這篇文章主要為大家詳細介紹了php微信高級接口群發(fā)、多客服的相關資料,感興趣的小伙伴們可以參考一下
    2016-06-06

最新評論