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 ...
随机推荐
- 《Windows核心编程》第1章——错误处理
GetLastError: GetLastError返回错误编码,即便出错函数后边跟随一个正确执行了的函数,也不会覆盖原先的错误代码: 考虑多线程的情况.子线程中的错误代码不会被主线程捕获: 但是子函 ...
- 再有人问你synchronized是什么,就把这篇文章发给他
在再有人问你Java内存模型是什么,就把这篇文章发给他.中我们曾经介绍过,Java语言为了解决并发编程中存在的原子性.可见性和有序性问题,提供了一系列和并发处理相关的关键字,比如synchronize ...
- 【BZOJ】【1014】【JLOI2008】火星人prefix
Splay/二分/Hash 看了网上的题目关键字(都不用点进去看……我也是醉了)了解到做法= =那就上呗,前面做了好几道Splay的题就是为了练手搞这个的. Hash判断字符串是否相同应该很好理解吧? ...
- Linux 挂载和卸载U盘
一般的U盘挂载方法: mount [-fnrsvw] [-t vfstype] [-o options] device dir 参数:device表示要挂载的设备,dir表示挂载点 -t 指定设备的 ...
- Go常用功能总结一阶段
1. go语言从键盘获取输入内容 <1. 最简单的办法是使用 fmt 包提供的 Scan 和 Sscan 开头的函数.请看以下程序: package main import "fmt& ...
- 【系统】supervisor支持多进程
[program:deployworker] directory = /etc/ansible/easyAnsible/app/deploy/ command = python Deploy.py p ...
- [Backbone] Customzing Backbone
Convert the AppointmentForm view below to use Mustache templating. Make sure you remember to change ...
- TotalCommander使用方法,如何对图片批量重命名
1 文件或文件夹重命名 F2 2 计算所有文件夹的大小 A/t+Shift+Enter.(这样对于文件的更新操作就更加快捷有效了,比如我的文档里面只有若干个子文件夹有更新,则别的都不用动,只要修改那些 ...
- SQL Sever 2008配置工具中过程调用失败解决方法
刚刚装了VS2013.然后打开数据库时,不管怎样也连不上.打开数据库配置,出现例如以下界面: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHU5MzAx ...
- Python 图形界面(GUI)设计
不要问我为什么要用 Python 来做这种事,我回到“高兴咋地”也不是不可以,总之好奇有没有好的解决方案.逛了一圈下来,总体上来说,Python 图形界面有以下几个可行度比较高的解决方案. 1. py ...