PHP原生:分享一个轻量级的缓存类=>cache.php
适用:原生PHP
cache.php
tips:代码最后有适用Demo哦。
<?php
/*
* 缓存类 cache
*/
define("cacheRoot","./cache/");
define("time",0);
define("name","");
define("cacheFileExt","php");
class cache {
//缓存目录
var $cacheRoot = "./cache/";
//缓存更新时间秒数,0为不缓存
var $cacheLimitTime = 0;
//缓存文件名
var $cacheFileName = "";
//缓存扩展名
var $cacheFileExt = "php";
/*
* 构造函数,文件名、有效期
* int $time 缓存更新时间
*/
function cache($name,$time ) {
if( intval( $time ) )
$this->cacheLimitTime = $time;
//$this->cacheLimitTime = $cacheLimitTime;
$this->cacheFileName = cacheRoot.$name.".".cacheFileExt; //如:/cache/order.php
ob_start();
}
/*
* 检查缓存文件是否在设置更新时间之内
* 返回:如果在更新时间之内则返回文件内容,反之则返回失败
*/
function cacheCheck(){
if( file_exists($this->cacheFileName) ) { //1、是否存在文件,使用绝对路径 ./cache/xx
$cTime = $this->getFileCreateTime($this->cacheFileName);
if( $cTime + $this->cacheLimitTime > time() ) { //2、文件是否有效
echo file_get_contents( $this->cacheFileName ); //输出文件内容到缓存内存中
$cacheContent = ob_get_contents(); //读取缓存内存中的数据
ob_end_flush(); return $cacheContent; //输出文件缓存
exit;
}else{ //过期,删除再新建
$r = @unlink( $this->cacheFileName );
return false;
}
}
return false;
}
/*
* 读取缓存文件或者输出静态:逻辑==》1、第一次缓存文件不存在,先写缓存,立马读缓存;2、读了之后马上创建文件file
* string $staticFileName 静态文件名(含相对路径)
*/
function caching( $staticFileName = "" ){
if( $this->cacheFileName ) {
$cacheContent = ob_get_contents();
ob_end_flush();
if( $staticFileName ) {
$this->saveFile( $staticFileName, $cacheContent );
}
if( $this->cacheLimitTime )
$this->saveFile( $this->cacheFileName, $cacheContent );
}
}
/*
* 清除缓存文件
* string $fileName 指定文件名(含函数)或者all(全部)
* 返回:清除成功返回true,反之返回false
*/
function clearCache( $fileName = "all" ) {
if( $fileName != "all" ) {
$fileName = $this->cacheRoot . strtoupper(md5($fileName)).".".$this->cacheFileExt;
if( file_exists( $fileName ) ) {
return @unlink( $fileName );
}else return false;
}
if ( is_dir( $this->cacheRoot ) ) {
if ( $dir = @opendir( $this->cacheRoot ) ) {
while ( $file = @readdir( $dir ) ) {
$check = is_dir( $file );
if ( !$check )
@unlink( $this->cacheRoot . $file );
}
@closedir( $dir );
return true;
}else{
return false;
}
}else{
return false;
}
}
/*根据当前动态文件生成缓存文件名*/
function getCacheFileName() {
return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
//return $this->cacheRoot . strtoupper(md5($_SERVER["REQUEST_URI"])).".".$this->cacheFileExt;
/* $_SERVER["QUERY_STRING"] 获取查询 语句,实例中可知,获取的是?后面的值
$_SERVER["REQUEST_URI"] 获取 http://localhost 后面的值,包括/aaa/index.php?p=222&q=333
$_SERVER["SCRIPT_NAME"] 获取当前脚本的路径,如:index.php
$_SERVER["PHP_SELF"] 当前正在执行脚本的文件名
*/
}
/*
* 缓存文件建立时间
* string $fileName 缓存文件名(含相对路径)
* 返回:文件生成时间秒数,文件不存在返回0
*/
function getFileCreateTime( $fileName ) {
if( ! trim($fileName) ) return 0;
if( file_exists( $fileName ) ) {
return intval(filemtime( $fileName ));
}else return 0;
}
/*
* 保存文件
* string $fileName 文件名(含相对路径)
* string $text 文件内容
* 返回:成功返回ture,失败返回false
*/
function saveFile($fileName, $text) {
if( ! $fileName || ! $text ) return false;
if( $this->makeDir( dirname( $fileName ) ) ) {
if( $fp = fopen( $fileName, "w" ) ) {
if( @fwrite( $fp, $text ) ) {
fclose($fp);
return true;
}else {
fclose($fp);
return false;
}
}
}
return false;
}
/*
* 连续建目录
* string $dir 目录字符串
* int $mode 权限数字
* 返回:顺利创建或者全部已建返回true,其它方式返回false
*/
function makeDir( $dir, $mode = "0777" ) {
if( ! $dir ) return 0;
$dir = str_replace( "\\", "/", $dir );
$mdir = "";
foreach( explode( "/", $dir ) as $val ) {
$mdir .= $val."/";
if( $val == ".." || $val == "." || trim( $val ) == "" ) continue;
if( ! file_exists( $mdir ) ) {
if(!@mkdir( $mdir, $mode )){
return false;
}
}
}
return true;
}
}
?> <?php
//使用DEMO
// include( "cache.php" );
// $cache = new cache(fileName,7100); //实例化对象:在/cache/目录下,创建一个fileName的文件,有效期7100秒
// $cache->cacheCheck(); //检查文件1、是否存在 2、是否有效;真=返回文件内容,假=返回false
// echo date("Y-m-d H:i:s");//写入缓存
// $cache->caching(); //没有参数将上面echo的时间先读出来,在保存缓存文件;有参数,输出参数对应的缓存内容
2、核心:
缓存的使用就是:
ob_start(); //开启缓存
echo XXX;
echo YY;
$cacheContent = ob_get_contents(); //获取上面echo的内容,所有内容组合到一起,成为字符串,如上面结果【XXXYY】
ob_end_flush(); //关闭缓存
最后,saveFile($fileName);保存文件名,参照上面代码。
PHP原生:分享一个轻量级的缓存类=>cache.php的更多相关文章
- Android 分享一个SharedPreferences的工具类,方便保存数据
我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPrefe ...
- 006 [翻译] Haneke(一个Swfit iOS缓存类)
Github项目地址:https://github.com/Haneke/HanekeSwift Haneke是一个用swift写成的轻量级iOS类,以简单好用著称(design-decisions- ...
- 分享一个自定义的 console 类,让你不再纠结JS中的调试代码的兼容
问题的产生 在写JS的过程中,为了调试我们常常会写很多 console.log.console.info.console.group.console.warn.console.error代码来查看JS ...
- 我也分享一个c# ini操作类
刚刚看了一篇 @云菲菲 的关于基于正则的INI辅助类文章:http://www.cnblogs.com/yunfeifei/p/4081977.html,作者写的不错.还看到评论处有一个的地址:htt ...
- 分享一个C#的分页类
废话不说只有代码: using System.Linq; using System.Collections.Generic; namespace CommonLibrary { public clas ...
- 分享一个Web弹框类
using System; using System.Text; namespace Core { /// <summary> /// MessageBox 的摘要说明. /// < ...
- 分享一个MD5加密工具类
来自:http://blog.csdn.net/zranye/article/details/8234480 Es:http://blog.csdn.net/longxibendi/article/d ...
- 分享一个单例模型类Singleton代码
相关代码: ; foreach (string key in dict.Keys) { if (cou ...
- 分享一个php加密字符串类。
class base64{ /** * 加密字符串 * @access static * @param string $data 字符串 * @param string $key 加密key * @r ...
随机推荐
- IOS学习之基于IOS7的tab bar
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28129473 作者:小马 什么是tabbar? 先几张图: 上图中蓝色框 ...
- 使用 SVWebViewController 推出浏览器控制器
SVWebViewController 简单翻译 https://github.com/samvermette/SVWebViewController SVWebViewController is a ...
- [Android Pro] AtomicInteger的用法
J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...
- Python 基础学习 总结篇
Python 基础学习总结 先附上所有的章节: Python学习(一)安装.环境配置及IDE推荐 Python学习(二)Python 简介 Python学习(三)流程控制 Python学习(四)数据结 ...
- vijos p1729 Knights
描述 在一个N*N的正方形棋盘上,放置了一些骑士.我们将棋盘的行用1开始的N个自然数标记,将列用'A'开始的N个大写英文字母标记.举个例子来说,一个标准的8*8的国际象棋棋盘的行标记为1..8,列标记 ...
- OpenCV学习(28) 轮廓
OpenCV中可以方便的在一副图像中检测到轮廓,并把这些轮廓画出来.主要用到两个函数:一个是findContours( img, contours0, hierarchy, RETR_TREE, CH ...
- 金蝶KIS下载地址
升级方法: 您好,建议您先升级到标准版7.5,再升级到标准版8.1,直接用7.5的软件打开金蝶2000的账套,会提示升级,再用8.1的软件打开7.5的账套,升级前,需先备份账套. 金蝶KIS标准版和业 ...
- Android高效加载大图、多图解决方案,有效避免程序内存溢出现象
好久没有写博客了,今天就先写一个小的关于在Android中加载大图如何避免内存溢出的问题. 后面会写如何使用缓存技术的核心类,android.support.v4.util.LruCache来加载图片 ...
- .Net应用程序打包部署总结
编译源代码并完成测试以后,开发过程其实并没有结束.在这个极端,需要把应用程序提供给用户.无论是ASP.NET应用程序,客户端应用程序还是 Compact Framework构建的应用程序,开发出来的软 ...
- Android GUI之View布局
在清楚了View绘制机制中的第一步测量之后,我们继续来了解分析View绘制的第二个过程,那就是布局定位.继续跟踪分析源码,根据之前的流程分析我们知道View的绘制是从RootViewImpl的perf ...