栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

简单实用的PHP文本缓存类实例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单实用的PHP文本缓存类实例

缓存在实际使用当中应用很广泛,可以减轻对服务器数据库的访问,提高运行速度。目前很多CMS内容管理系统中频繁使用缓存机制来提高系统运行的效率。下面是一个写得不错的缓存类,可以参考下缓存的机制与写法。

cache.inc.php

dir_isvalid($dir)) {
   $this->dir = $dir;
   $this->lifetime = $lifetime;
   $this->ext = '.Php';
   $this->cacheid = $this->getcacheid();
  }
 }
 
 private function isvalid() {
  if (!file_exists($this->cacheid)) return false;
  if (!(@$mtime = filemtime($this->cacheid))) return false;
  if (mktime() - $mtime > $this->lifetime) return false;
  return true;
 }
 
 public function write($mode=0,$content='') {
  switch ($mode) {
   case 0:
    $content = ob_get_contents();
    break;
   default:
    break;
  }
  ob_end_flush();
  try {
   file_put_contents($this->cacheid,$content);
  }
  catch (Exception $e) {
   $this->error('写入缓存失败!请检查目录权限!');
  }
 }
 
 public function load() {
  if ($this->isvalid()) {
   echo "This is Cache. ";
   //以下两种方式,哪种方式好?????
   require_once($this->cacheid);
   //echo file_get_contents($this->cacheid);
   exit();
  }
  else {
   ob_start();
  }
 }
 
 public function clean() {
  try {
   unlink($this->cacheid);
  }
  catch (Exception $e) {
   $this->error('清除缓存文件失败!请检查目录权限!');
  }
 }
 
 private function getcacheid() {
  return $this->dir.md5($this->geturl()).$this->ext;
 }
 
 private function dir_isvalid($dir) {
  if (is_dir($dir)) return true;
  try {
   mkdir($dir,0777);
  }
  catch (Exception $e) {
    $this->error('所设定缓存目录不存在并且创建失败!请检查目录权限!');
    return false;   
  }
  return true;
 }
 
 private function geturl() {
  $url = '';
  if (isset($_SERVER['REQUEST_URI'])) {
   $url = $_SERVER['REQUEST_URI'];
  }
  else {
   $url = $_SERVER['Php_SELF'];
   $url .= empty($_SERVER['QUERY_STRING'])?'':'?'.$_SERVER['QUERY_STRING'];
  }
  return $url;
 }
 
 private function error($str) {
  echo $str;
 }
}
?>

demo.php

load(); //装载缓存,缓存有效则不执行以下页面代码
 //页面代码开始
 echo date('H:i:s jS F');
 //页面代码结束
 $cache->write(); //首次运行或缓存过期,生成缓存
 ------------------------------------Demo2-------------------------------------------
 require_once('cache.inc.php');
 $cachedir = './Cache/'; //设定缓存目录
 $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
 if ($_GET['cacheact'] != 'rewrite') //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
  $cache->load(); //装载缓存,缓存有效则不执行以下页面代码
 //页面代码开始
 $content = date('H:i:s jS F');
 echo $content;
 //页面代码结束
 $cache->write(1,$content); //首次运行或缓存过期,生成缓存
 ------------------------------------Demo3-------------------------------------------
 require_once('cache.inc.php');
 define('CACHEENABLE',true);
 if (CACHEENABLE) {
  $cachedir = './Cache/'; //设定缓存目录
  $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
  if ($_GET['cacheact'] != 'rewrite') //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
   $cache->load(); //装载缓存,缓存有效则不执行以下页面代码 
 }
 //页面代码开始
 $content = date('H:i:s jS F');
 echo $content;
 //页面代码结束
 if (CACHEENABLE)
  $cache->write(1,$content); //首次运行或缓存过期,生成缓存
?>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对考高分网的支持。如果你想了解更多相关内容请查看下面相关链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/39261.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号