PHP快速缓存类

  1. <?php
  2. error_reporting(0);
  3. /
  4. // FastCache Lib
  5. // 快速缓存类,参考FleaPHP,加入了数据分页缓存数据功能
  6. // 需要将缓存目录设置为可写,默认的缓存目录为.cache目录
  7. /
  8. //定义缓存目录
  9. if(!defined('CACHE_DIR'))   define('CACHE_DIR','~cache');
  10. /**
  11.   * 写入文件内容(采用FleaPHP的代码)
  12.   */
  13. function safe_file_put_contents($filename, & $content) {
  14.     $fp = fopen($filename'w');
  15.     if (!$fp)   return false;
  16.     if (flock($fp, LOCK_EX))    fwrite($fp$content);
  17.     
  18.     flock($fp,LOCK_UN);
  19.     fclose($fp);
  20.     return true;
  21. }
  22. /**
  23.   * FastCache 缓存类
  24.   * @package cache
  25.   * @version 0.0.1: ExpCache.php 987 2007-10-19 16:34:24 KeleZYB $
  26.   */
  27. class FastCache 
  28. {
  29.     /**
  30.       * 写入缓存
  31.       * @param string $cacheKey 缓存ID,不同的缓存内容应该使用不同的ID
  32.       * @param maxed $data 缓存的数据(任何类型)
  33.       * @return bool 写入缓存是否成功
  34.       */
  35.     static function write($cacheKey,$data)
  36.     {   
  37.         $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '.php';
  38.         /*$c . iconv("GB2312", "UTF-8", var_export($data, true)) . ";/n?>";*/
  39.         if (!safe_file_put_contents($cacheFile, var_export($data,true))) {
  40.             return false;
  41.         } else {
  42.             return true;
  43.         }
  44.     }
  45.     /**
  46.       * 写入缓存
  47.       * @param string $cacheKey 缓存ID,不同的缓存内容应该使用不同的ID
  48.       * @param int $time 缓存过期时间
  49.       * @param bool $timeIsLifetime 指示 $time 参数的作用
  50.       * @return mixed 返回缓存的内容,缓存不存在或失效则返回 false
  51.       */
  52.     static function read($cacheKey$time = 900, $timeIsLifetime = false) 
  53.     {
  54.         $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '.php';
  55.         if (!file_exists($cacheFile)) { return false; }
  56.         if ($timeIsLifetime && $time == -1) {return require($cacheFile);}
  57.         $filetime = filemtime($cacheFile);
  58.         if ($timeIsLifetime) {
  59.             if (time() >= $filetime + $time) { return false; }
  60.         } else {
  61.             if ($time >= $filetime) { return false; }
  62.         }
  63.         return require($cacheFile);
  64.     }
  65.     /**
  66.       * 删除指定的缓存键的缓存
  67.       * @param string $cacheId
  68.       * @return boolean
  69.       */
  70.     static function delete($cacheKey
  71.     {
  72.         $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '.php';
  73.         if (file_exists($cacheFile)) {
  74.             return unlink($cacheFile);
  75.         }
  76.         return true;
  77.     }
  78.     /**
  79.       * 写入分页缓存
  80.       * @param string $cacheKey 缓存ID
  81.       * @param maxed $data 缓存的数据(任何类型)
  82.       * @param int $step 每页记录数
  83.       * @return bool 写入缓存是否成功
  84.       */
  85.     static function writePage($cacheKey,$data,$step = 10) 
  86.     {
  87.         if(is_array($data)) {
  88.             $_count = count($data);
  89.             if($_count <= $step) {
  90.                 return FastCache::write($cacheKey,$data);
  91.             } else {
  92.                 if($_count % $step ==0) {
  93.                     $_page = intval($_count / $step);
  94.                 } else {
  95.                     $_page = intval($_count / $step) +1;
  96.                 }
  97.                 $_pageinfo = array(
  98.                 'PageSize'  => $_page,   //页数
  99.                 'PageStep'  => $step,    //每页条数
  100.                 'RecordCount' => $_count   //记录总数
  101.                 );
  102.                 if(!is_dir(CACHE_DIR . '/' . md5($cacheKey))) {mkdir(CACHE_DIR . '/' . md5($cacheKey));}
  103.                 for($i = 1; $i<= $_page$i++) {
  104.                     $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '/_' . $i . '.php';
  105.                     unset($_tem);
  106.                     $_tem = array();
  107.                     $_start = $step * ($i-1);
  108.                     if($i == $_page) {
  109.                         if($_count % $step !=0) {
  110.                             $_end = $step * ($i-1) + $_count % $step;
  111.                         } else {
  112.                             $_end = $step * $i + $_count % $step;
  113.                         }
  114.                     } else {
  115.                         $_end = $step + $_start;
  116.                     }
  117.                     for($j = $_start$j < $_end$j++) {
  118.                         $_tem[] = $data[$j];
  119.                     }
  120.                     unset($contents);
  121.                     $c . iconv("GB2312""UTF-8", var_export($_tem, true)) . ";/n?>";
  122.                     if(safe_file_put_contents($cacheFile$contents)) {
  123.                         $_result = true;
  124.                     }
  125.                     else {
  126.                         return false;
  127.                     }
  128.                 }
  129.                 $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '/_page'  . '.php';
  130.                 $c . iconv("GB2312""UTF-8", var_export($_pageinfo, true)) . ";/n?>";
  131.                 if(safe_file_put_contents($cacheFile$contents)) {
  132.                     $_result = true;
  133.                 }
  134.                 else {
  135.                     return false;
  136.                 }
  137.                 return $_result;
  138.             }
  139.         }
  140.         else {
  141.             return FastCache::write($cacheKey,$data);
  142.         }
  143.     }
  144.     /**
  145.   * 读取缓存(按照页码读取)
  146.    * @param string $cacheKey 缓存ID
  147.    * @param int $page 读取该页码的缓存
  148.    * @param int $time 缓存过期时间
  149.    * @param bool $timeIsLifetime 指示 $time 参数的作用
  150.    *
  151.    * @return mixed 返回缓存的内容,缓存不存在或失效则返回 false
  152.   */
  153.     static function readPage($cacheKey,$page =1, $time = 900, $timeIsLifetime = false) {
  154.         $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '/_' . $page . '.php';
  155.         if (!file_exists($cacheFile)) { return false; } //缓存不存在
  156.         if ($timeIsLifetime && $time == -1) {
  157.             return require($cacheFile);
  158.         }
  159.         $filetime = filemtime($cacheFile);
  160.         if ($timeIsLifetime) {
  161.             if (time() >= $filetime + $time) { return false; }
  162.         }
  163.         else {
  164.             if ($time >= $filetime) { return false; }
  165.         }
  166.         return require($cacheFile);
  167.     }
  168.     /**
  169.   * 删除分页缓存
  170.   * @param string $cacheKey 缓存ID
  171.   */
  172.     static function deletePage($cacheKey) {
  173.         $_result = true;
  174.         $cacheFile = CACHE_DIR . "/" . md5($cacheKey) . "/*.php";
  175.         foreach (glob($cacheFileas $filename) {
  176.             if (file_exists($filename)) {
  177.                 unlink($filename);
  178.             }
  179.             else {
  180.                 $_result = false;
  181.             }
  182.         }
  183.         rmdir(CACHE_DIR . "/" . md5($cacheKey).'/');
  184.         return $_result;
  185.     }
  186.     /**
  187.   * 读取分页信息缓存
  188.   * @param string $cacheKey 缓存ID
  189.   */
  190.     static function readPageInfo($cacheKey) {
  191.         $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '/_page' . '.php';
  192.         if (!file_exists($cacheFile)) { return false; } //缓存不存在
  193.         return require($cacheFile);
  194.     }
  195. }
  196. //test
  197. $data = array(
  198. 'key1' => 'nihao中国',
  199. 'key2' => array(1,2,3,4,5),
  200. 'key3' => 23
  201. );
  202. $data1 = array();
  203. for($i=0;$i<10;$i++) {
  204.     $data1[] = str_repeat("ababac",rand(1, 15));
  205. }
  206. FastCache::write("hello",$data);
  207. FastCache::write("hello2",$data1);
  208. var_dump(FastCache::read('hello'));
  209. var_dump(FastCache::read('hello2'));
  210. //FastCache::delete('hello2');
  211. var_dump(FastCache::read('hello2'));
  212. FastCache::writePage("hello",array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21));
  213. print_r(FastCache::readPage("hello",2));
  214. //FastCache::deletePage('hello');
  215. FastCache::writePage("hello2","asfafasf");
  216. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值