国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php文件緩存類用法實例分析

2024-05-04 23:34:36
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php文件緩存類用法,以實例形式較為詳細(xì)的分析了php文件緩存類的定義、功能及具體使用技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php文件緩存類用法。分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2. /** 
  3. * 簡單的文件緩存類 
  4. * 
  5. */ 
  6. class XZCache{ 
  7. // default cache time one hour 
  8. var $cache_time = 3600; 
  9. // default cache dir 
  10. var $cache_dir = './cache'
  11. public function __construct($cache_dir=null, $cache_time=null){ 
  12. $this->cache_dir = isset($cache_dir) ? $cache_dir : $this->cache_dir; 
  13. $this->cache_time = isset($cache_time) ? $cache_time : $this->cache_time; 
  14. public function saveCache ($key$value){ 
  15. if (is_dir($this->cache_dir)){ 
  16. $cache_file = $this->cache_dir . '/xzcache_' . md5($key); 
  17. $timedif = @(time() - filemtime($cache_file)); 
  18. if ($timedif >= $this->cache_time) { 
  19. // cached file is too old, create new 
  20. $serialized = serialize($value); 
  21. if ($f = @fopen($cache_file'w')) { 
  22. fwrite ($f$serializedstrlen($serialized)); 
  23. fclose($f); 
  24. $result = 1; 
  25. }else
  26. echo "Error:dir is not exist."
  27. $result = 0; 
  28. return $result
  29. /** 
  30. * @return array  
  31. * 0 no cache 
  32. * 1 cached 
  33. * 2 overdue 
  34. */ 
  35. public function getCache ($key) { 
  36. $cache_file = $this->cache_dir . '/xzcache_' . md5($key); 
  37. if (is_dir($this->cache_dir) && is_file($cache_file)) { 
  38. $timedif = @(time() - filemtime($cache_file)); 
  39. if ($timedif >= $this->cache_time) { 
  40. $result['cached'] = 2; 
  41. }else
  42. // cached file is fresh enough, return cached array 
  43. $result['value'] = unserialize(file_get_contents($cache_file)); 
  44. $result['cached'] = 1; 
  45. }else { 
  46. echo "Error:no cache"
  47. $result['cached'] = 0; 
  48. return $result
  49. //end of class 

用法示例如下:

 

 
  1. $cache = new XZCache(); 
  2. $key = 'global'
  3. $value = $GLOBALS
  4. $cache->saveCache($key$value); 
  5. $result = $cache->getCache($key); 
  6. var_dump($result); 

希望本文所述對大家的php程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 海丰县| 河津市| 虹口区| 郴州市| 修文县| 沾化县| 合江县| 囊谦县| 无为县| 尤溪县| 勐海县| 全州县| 贵德县| 海宁市| 太谷县| 西和县| 丽水市| 灵石县| 灵璧县| 湖南省| 报价| 灵宝市| 清镇市| 托里县| 松滋市| 怀化市| 普定县| 建水县| 萨迦县| 彭水| 友谊县| 沁水县| 航空| 惠水县| 丰宁| 青州市| 迭部县| 芒康县| 高唐县| 宣汉县| 西吉县|