麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:39:46
字體:
來源:轉載
供稿:網友

這篇文章和大家分享了一個簡單至極的PHP緩存類代碼,緩存的應用對于php項目的開發來說尤為重要,需要的朋友可以參考下

網上關于 PHP 緩存類的資料很多,不過這個類應該是我見過功能滿足需求,但又無比簡潔的一個。廢話不多說,直接看代碼吧!

使用說明: 1、實例化

$cache = new Cache();

2、設置緩存時間和緩存目錄

$cache = new Cache(60, '/any_other_path/');

第一個參數是緩存秒數,第二個參數是緩存路徑,根據需要配置。

默認情況下,緩存時間是 3600 秒,緩存目錄是 cache/

3、讀取緩存

$value = $cache->get('data_key');

4、寫入緩存

$value = $cache->put('data_key', 'data_value');

完整實例:

 

 
  1. $cache = new Cache();  
  2.  
  3. //從緩存從讀取鍵值 $key 的數據  
  4. $values = $cache->get($key);  
  5.  
  6. //如果沒有緩存數據  
  7. if ($values == false) {  
  8. //insert code here...  
  9. //寫入鍵值 $key 的數據  
  10. $cache->put($key, $values);  
  11. else {  
  12. //insert code here...  
  13. }  

Cache.class.php

 

 
  1. <?php  
  2. class Cache {  
  3. private $cache_path;//path for the cache  
  4. private $cache_expire;//seconds that the cache expires  
  5.  
  6. //cache constructor, optional expiring time and cache path  
  7. public function Cache($exp_time=3600,$path="cache/"){  
  8. $this->cache_expire=$exp_time;  
  9. $this->cache_path=$path;  
  10. }  
  11.  
  12. //returns the filename for the cache  
  13. private function fileName($key){  
  14. return $this->cache_path.md5($key);  
  15. }  
  16.  
  17. //creates new cache files with the given data, $key== name of the cache, data the info/values to store  
  18. public function put($key, $data){  
  19. $values = serialize($data);  
  20. $filename = $this->fileName($key);  
  21. $file = fopen($filename, 'w');  
  22. if ($file){//able to create the file  
  23. fwrite($file, $values);  
  24. fclose($file);  
  25. }  
  26. else return false;  
  27. }  
  28.  
  29. //returns cache for the given key  
  30. public function get($key){  
  31. $filename = $this->fileName($key);  
  32. if (!file_exists($filename) || !is_readable($filename)){//can't read the cache  
  33. return false;  
  34. }  
  35. if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired  
  36. $file = fopen($filename, "r");// read data file  
  37. if ($file){//able to open the file  
  38. $data = fread($file, filesize($filename));  
  39. fclose($file);  
  40. return unserialize($data);//return the values  
  41. }  
  42. else return false;  
  43. }  
  44. else return false;//was expired you need to create new  
  45. }  
  46. }  
  47. ?> 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中文字幕在线成人 | 毛片免费大全短视频 | 久久久久久久久久久久久久久久久久 | 3级毛片 | 高清中文字幕在线 | 亚洲午夜在线 | 毛片118极品美女写真 | 成人爱爱电影 | 一级黄色av电影 | 免费在线观看午夜视频 | av在线成人 | 小视频免费在线观看 | 综合网日日天干夜夜久久 | 久久综合久久综合久久 | 久久久电影电视剧免费看 | 99精品视频在线观看免费播放 | 男人天堂新地址 | 免费在线中文字幕 | 国产日韩大片 | chinesehd天美原创xxxx | 亚洲一区成人在线 | 亚洲精品免费播放 | 暴力肉体进入hdxxxx古装 | 做羞羞视频 | 蜜桃网在线观看 | 免费一级a毛片在线播放视 日日草夜夜操 | 青青国产在线视频 | 毛片网站视频 | 中国女警察一级毛片视频 | 啪啪毛片 | 国产亚洲精品久久久久5区 日韩一级片一区二区三区 国产精品久久久久av | 亚洲福利在线视频 | 一区二区久久久久草草 | 国产va在线观看 | 中文字幕在线播放视频 | 亚洲成a| 欧美精品亚洲人成在线观看 | 日韩大片在线永久观看视频网站免费 | 国产精品一区视频 | 国产艳妇av视国产精选av一区 | 中国女警察一级毛片视频 |