仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 900|回复: 18
打印 上一主题 下一主题

[学习教程] PHP编程:对一个cache类的实践使用

[复制链接]
只想知道 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:10:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
会MYSQL吗?会,我会把我的信息在数据库里插入删除啦cache   Class_Cache.php:
<?php
  
class cache
{
  
var $cacheDirectory;
  
var $cacheDuration=3600;
  
var $cacheFilename;
  
function cache($cacheDuration=3600,$cacheDirectory='./cache')
{
$this->cacheDuration = 0;
$this->cacheDirectory = '.';
$this->cacheFilename = '';
  
$this->updateCache($cacheDuration,$cacheDirectory);
}
  
function getCacheFilename()
{
return $this->cacheFilename;
}
  
function updateCache($cacheDuration=3600,$cacheFolder='./cache')
{
$this->cacheDuration = $cacheDuration;
$this->cacheDirectory = $cacheFolder;
$this->_makeCacheFolder();
}
  
function _makeCacheFolder()
{
/*if (!is_dir($this->cacheDirectory))
{
$temp = explode('/',$this->cacheDirectory);
$cur_dir = '';
for($i=0;$i<count($temp);$i++)
{
$cur_dir .= $temp[$i].'/';
  
if (!is_dir($cur_dir))
{
if (@mkdir($cur_dir,777)&&($cur_dir!=getcwd()))
{
$this->_writeFile($cur_dir.'.htaccess','Deny from all');
$this->_writeFile($cur_dir.'index.html','');
}
}
}
}*/
if (!is_dir($this->cacheDirectory))
{
$cur_dir=$this->cacheDirectory;
//echo $cur_dir;
if (@mkdir($cur_dir,777))
{
$this->_writeFile($cur_dir.'.htaccess','Deny from all');
$this->_writeFile($cur_dir.'index.html','');
}
}
  
}
  
function _writeFile($filename,$contents)
{
if (!file_exists($filename))
{
$fp = @fopen($filename,'w');
if ($fp)
{
fputs($fp,$contents);
fclose($fp);
}
}else{
unlink($filename);
$fp = @fopen($filename,'w');
if ($fp)
{
fputs($fp,$contents);
fclose($fp);
}
}
}
  
function _setCacheFilename($contents)
{
//$this->cacheFilename = $this->cacheDirectory.'/'.md5($contents).'.txt';
  
/***********/
global $cache_file;
$this->cacheFilename = $this->cacheDirectory.'/'.$cache_file.'.txt';
/***********/
}
function returnCacheTime()
{
//return "asdfd";
$tim=filemtime($this->cacheFilename);
return date('Y年m月d日 H时i分s秒',$tim);
}
function inCache($contents,$sty='')
{
$this->_setCacheFilename($contents);
if($sty==1)
{
return file_exists($this->cacheFilename);
}else{
if(file_exists($this->cacheFilename))
{
$tim=filemtime($this->cacheFilename);
if((time()-$tim)>$this->cacheDuration)
{
return false;
}else{
return true;
}
}else{
return false;
}
}
}
  
function readCache()
{
$contents = '';
$fp = @fopen($this->cacheFilename,'r');
if ($fp)
{
while(!feof($fp))  
$contents .= fread($fp,4096);
fclose($fp);
}
return $contents;
}
  
function saveInCache($contents,$filename='')
{
if (trim($filename)=='') $filename = $contents;
if ($this->inCache($filename,1))
{
if((time()-filemtime($this->cacheFilename))>$this->cacheDuration)
{
@unlink($this->cacheFilename);
}
}
$this->_writeFile($this->cacheFilename,$contents);
}
  
}
?>
cache.php:
<?
require_once("Class_Cache.php");?>
<?
//---------页面缓存----------
$is_cache=1;//是不是缓存
$cache_time=300;//缓存工夫
if ((strstr($script_name,"/member/") == true) || (strstr($script_name,"/common/") == true))
$is_cache=0;
$cacheDirectory=$_SERVER['DOCUMENT_ROOT']."/cache/";
if($_SERVER['QUERY_STRING']=='')
$cache_file=$_SERVER['PHP_SELF'];
else
$cache_file=$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
if($_SERVER['PHP_SELF']=="/index.php")
$cache_file="___index.php";
$cache_file=preg_replace(array("/\//","/\?/"),array("",""),$cache_file);
//echo $cache_file;
  
if($is_cache==1)
{
$cache=new cache($cache_time,$cacheDirectory);
  
if($cache->incache($cache_file))
{
$output=$cache->readcache();
$CacheTime=$cache->returnCacheTime();
unset($cache);
//if( function_exists(return_execute_time()) )
$execute_time=return_execute_time();
$output=str_replace("<!--show_execute_time-->",$execute_time."<br>缓存版本:".$CacheTime,$output);
print($output);
exit;
}else
ob_start();
  
}
function all_cache()
{
global $is_cache;
global $cache_file;
global $cache;
if($is_cache==1)
{
//这里是输入的内容
  
$output = ob_get_clean();
ob_end_clean();  
$cache->saveInCache($output,$cache_file);  
$CacheTime=$cache->returnCacheTime();
unset($cache);
//if( function_exists(return_execute_time()) )
$execute_time=return_execute_time();
$output=str_replace("<!--show_execute_time-->",$execute_time."<br>缓存版本:".$CacheTime,$output);
print($output);
//exit;  
}  
}
?>
用法
在页面开首援用
<?
require("cache.php")?>
在页面最初加上
<?
all_cache();?>

实践使用http://www.scmetals.com
class_cache类 原贴:http://www.phpx.com/happy/thr83014.html
class_cache.php内容以下
<?php
  
class cache
{
     
    var $cacheDirectory;
     
    var $cacheDuration=3600;
     
    var $cacheFilename;
  
    function cache($cacheDuration=3600,$cacheDirectory='./cache')
    {
        $this->cacheDuration = 0;
        $this->cacheFilename = '';
        $this->cacheDirectory = '.';
        $this->updateCache($cacheDuration,$cacheDirectory);
    }
  
    function _makeCacheFolder()
    {
        if (!is_dir($this->cacheDirectory))
        {
            $temp = explode('/',$this->cacheDirectory);
            $cur_dir = '';
            for($i=0;$i<count($temp);$i++)
            {
                $cur_dir .= $temp[$i].'/';
                if (!is_dir($cur_dir))
                {
                    if (@mkdir($cur_dir,777)&&($cur_dir!=getcwd()))
                    {
                         $this->_writeFile($cur_dir.'.htaccess','Deny from all');
                         $this->_writeFile($cur_dir.'index.html','');
                    }
                }
            }
        }
         
    }
  
    function getCacheFilename()
    {
        return $this->cacheFilename;
    }
  
     function _setCacheFilename($contents)
     {
        $this->cacheFilename = $this->cacheDirectory.'/'.md5($contents).'.txt';
     }
  
     function inCache($contents,$sty='')
     {
         $this->_setCacheFilename($contents);
        if($sty==1)
         {
            return file_exists($this->cacheFilename);
         }
         else
         {
            if(file_exists($this->cacheFilename))
             {
                $tim=filemtime($this->cacheFilename);
                if((time()-$tim)>$this->cacheDuration)
                 {
                    return false;
                 }
                 else
                 {
                    return true;
                 }
             }
             else
             {
                 return false;
             }
         }
     }
  
     function readCache()
     {
         $contents = '';
         $fp = @fopen($this->cacheFilename,'r');
        if ($fp)
        {
            while(!feof($fp)) $contents .= fread($fp,4096);
            fclose($fp);
        }
        return $contents;
     }
      
    function updateCache($cacheDuration=3600,$cacheFolder='./cache')
    {
        $this->cacheDuration = $cacheDuration;
        $this->cacheDirectory = $cacheFolder;
        $this->_makeCacheFolder();
    }
     
     function saveInCache($contents,$filename='')
     {
            if (trim($filename)=='') $filename = $contents;
            if ($this->inCache($filename,1))
            {
                if((time()-filemtime($this->cacheFilename))>$this->cacheDuration)
                {
                    @unlink($this->cacheFilename);
                }
            }
            $this->_writeFile($this->cacheFilename,$contents);
     }
  
     function _writeFile($filename,$contents)
     {
         if (!file_exists($filename))
         {
             $fp = @fopen($filename,'w');
             if ($fp)
             {
                fputs($fp,$contents);
                fclose($fp);
             }
         }
        else
         {
            unlink($filename);
            $fp = @fopen($filename,'w');
             if ($fp)
             {
                fputs($fp,$contents);
                fclose($fp);
             }
         }
     }
  
}
?>

工具程序用来显示 Rasmus Lerdorf 的个人履历,以及统计网页流量。
简单生活 该用户已被删除
沙发
发表于 2015-2-4 09:49:53 | 只看该作者
Ps:以上纯属原创,如有雷同,纯属巧合
小魔女 该用户已被删除
板凳
发表于 2015-2-4 21:18:37 | 只看该作者
有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。
若相依 该用户已被删除
地板
发表于 2015-2-5 12:11:13 | 只看该作者
兴趣是最好的老师,百度是最好的词典。
不帅 该用户已被删除
5#
发表于 2015-2-7 03:30:02 | 只看该作者
不禁又想起那些说php是草根语言的人,为什么认得差距这么大呢。
金色的骷髅 该用户已被删除
6#
发表于 2015-3-4 09:39:32 | 只看该作者
找到的的资料很多都是在论坛里的,需要注册,所以我一般没到一个论坛都注册一个id,所有的id都注册成一样的,这样下次再进来的时候就不用重复注册啦。当然有些论坛的某些资料是需要的付费的。
冷月葬花魂 该用户已被删除
7#
发表于 2015-3-9 01:04:08 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
柔情似水 该用户已被删除
8#
发表于 2015-3-11 21:15:25 | 只看该作者
Ps:以上纯属原创,如有雷同,纯属巧合
变相怪杰 该用户已被删除
9#
发表于 2015-3-13 23:07:18 | 只看该作者
作为一个合格的coder 编码的规范是必须,命名方面我推崇“驼峰法”,另外就是自己写的代码最好要带注释,不然时间长了,就算是自己的代码估计看起来都费事,更不用说别人拉。
精灵巫婆 该用户已被删除
10#
发表于 2015-3-24 15:46:49 | 只看该作者
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
飘灵儿 该用户已被删除
11#
发表于 2015-3-25 03:03:03 | 只看该作者
如果你可以写完像留言板这样的程序,那么你可以去一些别人的代码了,
莫相离 该用户已被删除
12#
发表于 2015-3-26 11:23:27 | 只看该作者
遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
老尸 该用户已被删除
13#
发表于 2015-4-17 19:26:51 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
兰色精灵 该用户已被删除
14#
发表于 2015-4-21 02:21:47 | 只看该作者
我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。
若天明 该用户已被删除
15#
发表于 2015-4-28 18:28:54 | 只看该作者
我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:
小女巫 该用户已被删除
16#
发表于 2015-5-6 14:09:38 | 只看该作者
首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。
17#
发表于 2015-5-8 15:19:40 | 只看该作者
学好程序语言,多些才是王道,写两个小时代码的作用绝对超过看一天书,这个我是深有体会(顺便还能练打字速度)。
谁可相欹 该用户已被删除
18#
发表于 2015-6-23 21:28:52 | 只看该作者
遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
第二个灵魂 该用户已被删除
19#
发表于 2015-7-11 08:35:10 | 只看该作者
如果你可以写完像留言板这样的程序,那么你可以去一些别人的代码了,
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-2 04:05

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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