现在的位置: 首页 > 综合 > 正文

缓存类,支持数据分页缓存

2013年12月08日 ⁄ 综合 ⁄ 共 5734字 ⁄ 字号 评论关闭
(缓存类,支持数据分页缓存).用来缓存数据,也可以一次性查询数据库之后缓存,下次查询数据时读取按照分页读取缓存.
本人的第一主题.希望大家多多支持,这个是我参考FleaPHP的缓存写出来的.
复制PHP内容到剪贴板
PHP代码:

<?php
/////////////////////////////////////////////////////////////////////////////
// FastCache Lib
// 快速缓存类,参考FleaPHP,加入了数据分页缓存数据功能
// 需要将缓存目录设置为可写,默认的缓存目录为.cache目录
// Copyright (c) 2007 - 2008 KeleZYB China Inc. ([url=http://www.52cookbook.com/]http://www.52cookbook.com[/url])
/////////////////////////////////////////////////////////////////////////////

  //定义缓存目录
  if(!defined('CACHE_DIR')) {
   define('CACHE_DIR','.cache');
  }
 
  /**
   * 写入文件内容(采用FleaPHP的代码)
  */
  function safe_file_put_contents($filename, & $content) {
     $fp = fopen($filename, 'w');
     if (!$fp) { return false; }
     if (flock($fp, LOCK_EX)) {
         fwrite($fp, $content);
     }
     fclose($fp);
     return true;
  }
 
  /**
  * FastCache 缓存类
  *
  * @copyright (c) 2007 - 2008 KeleZYB China Inc. ([url=http://www.52cookbook.com/]http://www.52cookbook.com[/url])
  * @author 冰软科技
  * @package cache
  * @version 0.0.1: ExpCache.php 987 2007-10-19 16:34:24 KeleZYB $
  */
  class FastCache {
  
   /**
   * 写入缓存
   * @param string $cacheKey 缓存ID,不同的缓存内容应该使用不同的ID
   * @param maxed $data 缓存的数据(任何类型)
   *
   * @return bool 写入缓存是否成功
   */
   static function write($cacheKey,$data) {
    $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '.php';
   
    $c . iconv("GB2312", "UTF-8", var_export($data, true)) . ";/n?>";
    if (!safe_file_put_contents($cacheFile, $contents)) {
             return false;
         }
         else {
             return true;
         }
   }
  
   /**
   * 写入缓存
   * @param string $cacheKey 缓存ID,不同的缓存内容应该使用不同的ID
   * @param int $time 缓存过期时间
   * @param bool $timeIsLifetime 指示 $time 参数的作用
   *
   * @return mixed 返回缓存的内容,缓存不存在或失效则返回 false
   */
   static function read($cacheKey, $time = 900, $timeIsLifetime = false) {
    $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '.php';
    if (!file_exists($cacheFile)) { return false; }
   
    if ($timeIsLifetime && $time == -1) {
             return require($cacheFile);
         }
        
         $filetime = filemtime($cacheFile);
         if ($timeIsLifetime) {
             if (time() >= $filetime + $time) { return false; }
         }
         else {
             if ($time >= $filetime) { return false; }
         }
         return require($cacheFile);
  }
 
  /**
  * 删除指定的缓存键的缓存
  * @param string $cacheId
  *
  * @return boolean
  */
  static function delete($cacheKey) {
   $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '.php';
     
      if (file_exists($cacheFile)) {
       return unlink($cacheFile);
      }
      return true;
  }
 
  /**
  * 写入分页缓存
  * @param string $cacheKey 缓存ID
   * @param maxed $data 缓存的数据(任何类型)
  * @param int $step 每页记录数
  *
   * @return bool 写入缓存是否成功
  */
  static function writePage($cacheKey,$data,$step = 10) {
   if(is_array($data)) {
    $_count = count($data);
    if($_count <= $step) {
     return FastCache::write($cacheKey,$data);
    }
    else {
     if($_count % $step ==0) {
      $_page = intval($_count / $step);
     }
     else {
      $_page = intval($_count / $step) +1;
     }
    
     $_pageinfo = array(
      'PageSize'  => $_page,   //页数
      'PageStep'  => $step,    //每页条数
      'RecordCount' => $_count   //记录总数
     );
     if(!is_dir(CACHE_DIR . '/' . md5($cacheKey))) {
      mkdir(CACHE_DIR . '/' . md5($cacheKey));
     }
     for($i = 1; $i<= $_page; $i++) {
      $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '/_' . $i . '.php';
      unset($_tem);
      $_tem = array();
      $_start = $step * ($i-1);
      if($i == $_page) {
       if($_count % $step !=0) {
        $_end = $step * ($i-1) + $_count % $step;
       }
       else {
        $_end = $step * $i + $_count % $step;
       }
      }
      else {
       $_end = $step + $_start;
      }
      for($j = $_start; $j < $_end; $j++) {
        $_tem[] = $data[$j];
      }
      unset($contents);
      $c . iconv("GB2312", "UTF-8", var_export($_tem, true)) . ";/n?>";
       if(safe_file_put_contents($cacheFile, $contents)) {
        $_result = true;
       }
       else {
        return false;
       }
     }
     $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '/_page'  . '.php';
     $c . iconv("GB2312", "UTF-8", var_export($_pageinfo, true)) . ";/n?>";
     if(safe_file_put_contents($cacheFile, $contents)) {
       $_result = true;
      }
      else {
       return false;
      }
     return $_result;
    }
   }
   else {
    return FastCache::write($cacheKey,$data);
   }
  }
 
  /**
  * 读取缓存(按照页码读取)
   * @param string $cacheKey 缓存ID
   * @param int $page 读取该页码的缓存
   * @param int $time 缓存过期时间
   * @param bool $timeIsLifetime 指示 $time 参数的作用
   *
   * @return mixed 返回缓存的内容,缓存不存在或失效则返回 false
  */
  static function readPage($cacheKey,$page =1, $time = 900, $timeIsLifetime = false) {
   $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '/_' . $page . '.php';
   if (!file_exists($cacheFile)) { return false; } //缓存不存在
  
   if ($timeIsLifetime && $time == -1) {
             return require($cacheFile);
         }
        
         $filetime = filemtime($cacheFile);
         if ($timeIsLifetime) {
             if (time() >= $filetime + $time) { return false; }
         }
         else {
             if ($time >= $filetime) { return false; }
         }
         return require($cacheFile);
  }
 
  /**
  * 删除分页缓存
  * @param string $cacheKey 缓存ID
  */
  static function deletePage($cacheKey) {
   $_result = true;
   $cacheFile = CACHE_DIR . "/" . md5($cacheKey) . "/*.php";
   foreach (glob($cacheFile) as $filename) {
       if (file_exists($filename)) {
        unlink($filename);
       }
       else {
        $_result = false;
       }
   }
   rmdir(CACHE_DIR . "/" . md5($cacheKey).'/');
   return $_result;
  }
 
  /**
  * 读取分页信息缓存
  * @param string $cacheKey 缓存ID
  */
  static function readPageInfo($cacheKey) {
   $cacheFile = CACHE_DIR . '/' . md5($cacheKey) . '/_page' . '.php';
   if (!file_exists($cacheFile)) { return false; } //缓存不存在
         return require($cacheFile);
  }
  }
?>
[php]

测试代码:
<?php
require_once('FastCache.php');
$data = array(
  'key1' => 'nihao中国',
  'key2' => array(1,2,3,4,5),
  'key3' => 23
);
$data1 = array();
for($i=0;$i<1000;$i++) {
  $data1[] = str_repeat("ababac",rand(1, 15));
}
FastCache::write("hello",$data);
FastCache::write("hello2",$data1);
var_dump(FastCache::read('hello'));
var_dump(FastCache::read('hello2'));
FastCache::delete('hello2');
var_dump(FastCache::read('hello2'));
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));
print_r(FastCache::readPage("hello",2));
FastCache::deletePage('hello');
FastCache::writePage("hello2","asfafasf");
?>
 

抱歉!评论已关闭.