PHPIN.NET

 找回密码
 立即注册
查看: 5189|回复: 0

[PHP类\函数] PHP缓存操作实例详解

[复制链接]

469

主题

31

回帖

5507

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5507
发表于 2014-12-19 17:29:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
PHP缓存操作实例详解

为什么要使用缓存技术?理由很简单:提高效率。在程序开发中,获取信息的方式主要是查询数据库,除此以外,也可能是通过Web Services或者别的某种方法,无论哪种方法,在大量的并发访问面前,它们都可能成为效率的瓶颈,为了解决这些问题,人们提出了很多解决方案,其中一些是利用优化软件(如:APC,Eaccelerator,Zend Optimizer等等)来提高程序的运行效率,合理的运用这些软件,往往能使程序的运行效率得到数量级上的提升,但前提是你必须拥主机的控制权,以便能够安装这些软件,如果你使用的是虚拟主机的话,那么只能祈祷你的服务提供商已经预装了某个优化软件,否则就必须自己使用PHP来实现相应的缓存功能。如果这让你感到无所适从,相信下面的这段缓存操作类的代码能给你一些有用的启发。(PHP缓存操作实例详解)
  1. <?php
  2. /**
  3. +----------------------------------------------------------  
  4. * franklin 缓存操作类  
  5. +----------------------------------------------------------  
  6. * 文件名称  cache.php  
  7. +----------------------------------------------------------  
  8. * 文件描述  缓存操作类  
  9. +----------------------------------------------------------  
  10. */
  11. class cache{
  12. //查询参数
  13. protected $options=array();
  14. //缓存次数
  15. protected $cacheCount=0;
  16. //缓存时间
  17. protected $cachetime=60;
  18. //缓存路径
  19. protected $cachePath='cache/';
  20. //数据返回类型, 1代表数组, 2代表对象
  21. protected $returnType=1;
  22. /**  
  23. * 读取缓存  
  24. * @param string $id  缓存名称  
  25. * @param int $time 缓存有效时间,默认为60秒,0为永远失效  
  26. * @param string $path缓存文件存放路径  
  27. * @accesspublic readCache  
  28. * @returnmixed如果读取成功返回缓存内容, 否则返回NULL  
  29. */  
  30. public function readCache($id,$time,$Path=''){
  31.     $id=md5($id);
  32.     $this->cachePath=emptyempty($Path)?$this->cachePath:$Path;
  33.     $this->cachetime=emptyempty($time)?$this->cachetime:$time;
  34.     $file=$this->cachePath.$id.'.php';
  35.     if(file_exists($file)){
  36.         //缓存过期
  37.         if((filemtime($file)+$time)<time()){
  38.             @unlink($file);
  39.             return NULL;
  40.         }
  41.         if(1===$this->returnType){
  42.             $row=include $file;
  43.         }else{
  44.             $data=file_get_contents($file);
  45.             $row=unserialize($data);
  46.         }
  47.         return $row;
  48.     }
  49.     return NULL;
  50. }
  51. /**  
  52. * 写入缓存  
  53. *  
  54. * @accesspublic  
  55. * @param mixed$data缓存内容  
  56. * @returnbool是否写入成功  
  57. */  

  58. public function writeCache($id,$data,$Path=''){
  59.     $this->cacheCount++;
  60.     $id=md5($id);
  61.     $this->cachePath=emptyempty($Path)?$this->cachePath:$Path;
  62.     $file=$this->cachePath.$id.'.php';
  63.     chmod($this->cachePath,777);
  64.     if(1===$this->returnType){
  65.         $data='<?php return '.var_export($data, TRUE).'; ?>';
  66.     }else{
  67.         $data=serialize($data);
  68.     }
  69.     return file_put_contents($file, $data);
  70. }
  71. /**  
  72. * 删除指定缓存  
  73. *  
  74. * @accesspublic  
  75. * @param mixed$id缓存名称  
  76. * @returnbool是否删除成功  
  77. */  
  78. public function delCache($id,$Path=''){  
  79.     $id=md5($id);
  80.     $this->cachePath=emptyempty($Path)?$this->cachePath:$Path;
  81.     $file=$this->cachePath.$id.'.php';
  82.     if(file_exists($file)){
  83.         return unlink($file);
  84.     }
  85.     return NULL;
  86. }
  87. /**  
  88. * 删除所有缓存  
  89. *  
  90. * @accesspublic  
  91. * @param mixed$dir缓存名称  
  92. * @returnbool清除所有缓存是否成功  
  93. */  
  94. public function delAllCache($Path=''){  
  95.     $id=md5($id);
  96.     $this->cachePath=emptyempty($Path)?$this->cachePath:$Path;
  97.     $path=$this->cachePath;
  98.     if(is_dir($path)){
  99.         if($dh=opendir($path)){
  100.             while(($file=readdir($dh))!==false){
  101.                 if($file!='..'&$file!='.'){
  102.                     if(is_dir($path.'/'.$file)){
  103.                         if(!delDir($path.'/'.$file)){
  104.                             return 0;
  105.                         }
  106.                     }else{
  107.                         if(!unlink($path.'/'.$file)){
  108.                             return 0;
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.             closedir($dh);
  114.         }
  115.         return 1;
  116.     }
  117. }
  118. }
复制代码
以上缓存操作类的引用方法如下:
  1. <?php
  2. include('cache.php');
  3. $data=array('http://www.phpin.net','http://www.baidu.com','http://www.google.cn');
  4. $cache=new cache();
  5. $id='test';
  6. //写入缓存
  7. $cache->writeCache($id,$data);
  8. //读缓存并打印
  9. $name_row=$cache->readCache($id,120);
  10. print_r($name_row);
  11. //删除某个变量
  12. $cache->delCache($id);
  13. //删除全部缓存
  14. $cache->delAllCache();
复制代码
注意要保证cache目录(即缓存目录)存在并且可写。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-4-18 19:13

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表