PHP缓存类

  1. <?php
  2. /**
  3.  * @name cache
  4.  * @deprecated control to cache data, $cache_out_time is a array to save cache date time out
  5.  * @author caleng
  6.  * @since 2008-08-05
  7.  * @version 1.0
  8.  */
  9. class cache
  10. {
  11.     private $cache_dir;         //缓存文件所放的相对网站目录的父目录
  12.     private $expireTime = 180;  //缓存的数据过期的时间,180秒
  13.     
  14.     function __construct($cache_dirname
  15.     {
  16.         if (!is_dir($cache_dirname)) {
  17.             if (!mkdir($cache_dirname,0777)) {
  18.                 $this->warn('Can not create the cache file.');
  19.                 return false;
  20.             }
  21.         }
  22.         
  23.         $this->cache_dir = $cache_dirname;
  24.     }
  25.     
  26.     function __destruct()
  27.     {
  28.         echo 'Cache class bye.';
  29.     }
  30.     
  31.     function get_url()
  32.     {
  33.         if (!isset($_SERVER['REQUEST_URI'])) {
  34.             $url = $_SERVER['REQUEST_URI'];
  35.         } else {
  36.             $url  = $_SERVER['SCRIPT_NAME'];
  37.             $url .= (!emptyempty($_SERVER['QUERY_STRING']))?'?'.$_SERVER['QUERY_STRING']:'';
  38.         }
  39.         return $url;
  40.     }
  41.     
  42.     function warn($errorstring)
  43.     {
  44.         echo "<B><font color='red'>Error:<pre>$errorstring</pre></font></B>";
  45.     }
  46.     
  47.     function cache_page($pageurl,$pagedata)
  48.     {
  49.         if (!$fso=fopen($pageurl'w')) {
  50.             $this->warn('Can not open the cache file.');
  51.             return false;
  52.         }
  53.         
  54.         //LOCK_EX:要取得独占锁定(写入的程序)
  55.         if (!flock($fso,LOCK_EX)) {
  56.             $this->warn('Can not lock the cache file.');
  57.             return false;
  58.         }
  59.         
  60.         if (!fwrite($fso$pagedata)) {
  61.             $this->warn('Can not write cache file.');
  62.             return false;
  63.         }
  64.         
  65.         //LOCK_UN:要释放锁定(无论共享或独占)
  66.         flock($fso, LOCK_UN);
  67.         fclose($fso);
  68.         return true;
  69.     }
  70.     
  71.     function display_cache($cacheFile)
  72.     {
  73.         if (!file_exists($cacheFile)) {
  74.             $this->warn('Can not display the cache file.');
  75.             return false;
  76.         }
  77.         
  78.         $fso = fopen($cacheFile'r');
  79.         $data = fread($fsofilesize($cacheFile));
  80.         fclose($fso);
  81.         return $data;
  82.     }
  83.     
  84.     function readData($cacheFile = 'default_cache.txt')
  85.     {
  86.         $cacheFile = $this->cache_dir.'/'.$cacheFile;
  87.         if (file_exists($cacheFile) && filemtime($cacheFile)>(time()-$this->expireTime)) {
  88.             $data = $this->display_cache($cacheFile);
  89.         } else {
  90.             $data="from here wo can get it from mysql database,update time is <b>".date('l dS /of F Y h:i:s A')."</b>,过期时间是:".date('l dS /of F Y h:i:s A',time()+$this->expireTime)."----------";
  91.             $this->cache_page($cacheFile,$data);
  92.         }
  93.         return $data;
  94.     }
  95. }
  96. /*Test
  97. $test = new cache('D:/wwwroot/Test');
  98. $data = $test->readData();
  99. print_r($data);
  100. */
  101. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值