- <?php
- error_reporting(0);
- /
- // FastCache Lib
- // 快速缓存类,参考FleaPHP,加入了数据分页缓存数据功能
- // 需要将缓存目录设置为可写,默认的缓存目录为.cache目录
- /
- //定义缓存目录
- 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);
- flock($fp,LOCK_UN);
- fclose($fp);
- return true;
- }
- /**
- * FastCache 缓存类
- * @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, var_export($data,true))) {
- 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);
- }
- }
- //test
- $data = array(
- 'key1' => 'nihao中国',
- 'key2' => array(1,2,3,4,5),
- 'key3' => 23
- );
- $data1 = array();
- for($i=0;$i<10;$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");
- ?>
PHP快速缓存类
最新推荐文章于 2024-07-29 22:50:36 发布