在这里,我们将给大家分享关于ThinkPHP文件缓存类代码分享的知识,让您更了解thinkphp缓存的本质,同时也会涉及到如何更有效地php数据文件缓存类代码实例、php文件缓存类、php文件缓存类实
在这里,我们将给大家分享关于ThinkPHP文件缓存类代码分享的知识,让您更了解thinkphp 缓存的本质,同时也会涉及到如何更有效地php数据文件缓存类代码实例、php文件缓存类、php文件缓存类实例整理、PHP文件缓存类实现代码的内容。
本文目录一览:ThinkPHP文件缓存类代码分享(thinkphp 缓存)
取自ThinkPHP的文件缓存类代码,这里就不多废话了,小伙伴们自己看注释吧。
}
/**
* @desc 设置<a href="https://www.jb51.cc/tag/wenjian/" target="_blank">文件</a>缓存
* @p<a href="https://www.jb51.cc/tag/ara/" target="_blank">ara</a>m string $key <a href="https://www.jb51.cc/tag/wenjian/" target="_blank">文件</a>名
* @p<a href="https://www.jb51.cc/tag/ara/" target="_blank">ara</a>m unkonw $data 缓存数据
* @p<a href="https://www.jb51.cc/tag/ara/" target="_blank">ara</a>m int $expire 过期时间
*/
public function set($key,$data,$expire = 0){
$this->filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT;
if(file_exists($this->filename)){
$res = $this->get($key);
if(md5($res) == md5(json_encode($data) ) ){
return true;
}
}
if(!is_dir(dirname($this->filename))){
mkdir(dirname($this->filename),0777);
}
$source = fopen($this->filename,'w+');
fwrite($source,json_encode($data));
fclose($source);
}
/**
* @desc <a href="https://www.jb51.cc/tag/huoqu/" target="_blank">获取</a><a href="https://www.jb51.cc/tag/wenjian/" target="_blank">文件</a>
* @p<a href="https://www.jb51.cc/tag/ara/" target="_blank">ara</a>m string $key <a href="https://www.jb51.cc/tag/wenjian/" target="_blank">文件</a>名
*/
public function get($key){
//$filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT;
if(!file_exists($this->filename)){
return '缓存<a href="https://www.jb51.cc/tag/wenjian/" target="_blank">文件</a>已经不存在';
}else{
$res = file_get_contents($this->filename);
}
return $res;
}
/**
* @desc <a href="https://www.jb51.cc/tag/shanchu/" target="_blank">删除</a><a href="https://www.jb51.cc/tag/wenjian/" target="_blank">文件</a>
* @p<a href="https://www.jb51.cc/tag/ara/" target="_blank">ara</a>m string $key <a href="https://www.jb51.cc/tag/wenjian/" target="_blank">文件</a>名
*/
public function del($key){
unlink($this->filename);
}
}
$data = array('name'=>'song','age'=>20,'sex'=>'man','favority'=>array('apple','banana'));
$cache = new Cache();
$cache->set('cache',$data);
//$cache->get('cache');
//$cache->del('cache');
php数据文件缓存类代码实例
php文件缓存类
php文件缓存 PHP ?php/** * 简单的文件缓存类 usage $cache = new XZCache();$key = ''global'';$value = $GLOBALS;$cache-saveCache($key, $value);$result = $cache-getCache($key);var_dump($result); * */ class XZCache{// default cache time one hourvar
php文件缓存 PHP
<?php /** * 简单的文件缓存类 usage $cache = new XZCache(); $key = ''global''; $value = $GLOBALS; $cache->saveCache($key, $value); $result = $cache->getCache($key); var_dump($result); * */ class XZCache{ // default cache time one hour var $cache_time = 3600; // default cache dir var $cache_dir = ''./cache''; public function __construct($cache_dir=null, $cache_time=null){ $this->cache_dir = isset($cache_dir) ? $cache_dir : $this->cache_dir; $this->cache_time = isset($cache_time) ? $cache_time : $this->cache_time; } public function saveCache ($key, $value){ if (is_dir($this->cache_dir)){ $cache_file = $this->cache_dir . ''/xzcache_'' . md5($key); $timedif = @(time() - filemtime($cache_file)); if ($timedif >= $this->cache_time) { // cached file is too old, create new $serialized = serialize($value); if ($f = @fopen($cache_file, ''w'')) { fwrite ($f, $serialized, strlen($serialized)); fclose($f); } } $result = 1; }else{ echo "Error:dir is not exist."; $result = 0; } return $result; } /** * @return array * 0 no cache * 1 cached * 2 overdue */ public function getCache ($key) { $cache_file = $this->cache_dir . ''/xzcache_'' . md5($key); if (is_dir($this->cache_dir) && is_file($cache_file)) { $timedif = @(time() - filemtime($cache_file)); if ($timedif >= $this->cache_time) { $result[''cached''] = 2; }else{ // cached file is fresh enough, return cached array $result[''value''] = unserialize(file_get_contents($cache_file)); $result[''cached''] = 1; } }else { echo "Error:no cache"; $result[''cached''] = 0; } return $result; } } //end of class
php文件缓存类实例整理
缓存类是我们开发应用中会常用使用到的功能,下面我来给大家整理几个php文件缓存类了,各个文件缓存类写法不同,但在性能上会有区别,有兴趣测试的朋友可测试一下这些缓存类吧。
例1
代码如下 | 复制代码 |
$fzz = new fzz_cache; //写入缓存
<br><span>error:</span><br> ".print_r($err->getTrace(),1)."<br> 登录后复制 if($debug == true) { file_put_contents(date(''Y-m-d H_i_s'').".log",$str); return $str; }else{ die($str); } } } ?> |
例2.从CI社区的stblog和CI的file_helper类中提取出来的php文件缓存类,一个简单的基于文件的key->value缓存类。
这个类可以用来缓存一些基本信息,比如博客的header,footer,sidebar中的一些不经常变化,从数据库中取出的
内容,取数据前先判断文件缓存中的内容是否过期,如果没过期取出来,过期了则连接数据库查询,并将结果重新
写入文件缓存,更新过期时间。跟memcache使用类似,不过更方便。用在一些小的应用上足够了...代码如下
立即学习“PHP免费学习笔记(深入)”;
代码如下 | 复制代码 |
define(''DIRECTORY_SEPARATOR'',''/''); /** /** if ( ! $fp = @fopen($file, FOPEN_READ)) flock($fp, LOCK_UN);//释放锁 return $data;
|
例3.自己觉得很好用的php文件缓存
代码如下 | 复制代码 |
class cache protected $_options = array( /* 初始化设置cache的配置信息什么的 */ //模式1 缓存存储方式 if(!$row = cache::get(''zj2'')) print_r($row);
/** /** 存入数据
$config = array( //第一个参数 缓存data 载入数据
//只有一个参数 cache_id 清空缓存
//清空指定缓存 cache信息配置 //在执行所有cache_func前调用 $_options = array( //再执行 就会按着新配置信息执行,否则是默认信息
|
PHP文件缓存类实现代码
这篇文章主要介绍了PHP文件缓存类实现代码,php中缓存分类数据库缓存,文件缓存和内存缓存,对php缓存感兴趣的朋友可以学习学习下面的文章。
php中缓存分类数据库缓存,文件缓存和内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考。
页面缓存类
代码如下 :
cacheCheck(); echo date("Y-m-d H:i:s"); $cache->caching(); */ class cache { //缓存目录 var $cacheRoot = "./cache/"; //缓存更新时间秒数,0为不缓存 var $cacheLimitTime = 3; //缓存文件名 var $cacheFileName = ""; //缓存扩展名 var $cacheFileExt = "php"; /* * 构造函数 * int $cacheLimitTime 缓存更新时间 */ function cache( $cacheLimitTime ) { if( intval( $cacheLimitTime ) ) $this->cacheLimitTime = $cacheLimitTime; $this->cacheFileName = $this->getCacheFileName(); ob_start(); } /* * 检查缓存文件是否在设置更新时间之内 * 返回:如果在更新时间之内则返回文件内容,反之则返回失败 */ function cacheCheck(){ if( file_exists( $this->cacheFileName ) ) { $cTime = $this->getFileCreateTime( $this->cacheFileName ); if( $cTime + $this->cacheLimitTime > time() ) { echo file_get_contents( $this->cacheFileName ); ob_end_flush(); exit; } } return false; } /* * 缓存文件或者输出静态 * string $staticFileName 静态文件名(含相对路径) */ function caching( $staticFileName = "" ){ if( $this->cacheFileName ) { $cacheContent = ob_get_contents(); //echo $cacheContent; 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; } /* * 缓存文件建立时间 * 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( "", "http://www.jb51.net/", $dir ); $mdir = ""; foreach( explode( "http://www.jb51.net/", $dir ) as $val ) { $mdir .= $val."http://www.jb51.net/"; if( $val == ".." || $val == "." || trim( $val ) == "" ) continue; if( ! file_exists( $mdir ) ) { if(!@mkdir( $mdir, $mode )){ return false; } } } return true; } } ?>
上面使用算是页面缓存了,每次访问页面的时候,,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示页面并同时生成缓存页面文件,这样下次访问的时候页面文件就发挥作用了。(模板引擎和网上常见的一些缓存类通常有此功能)
给大家介绍一个Memcache缓存,算是内存缓存。
代码如下
connect(''localhost'', 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server''s version: ".$version."n"; $tmp_object = new stdClass; $tmp_object->str_attr = ''test''; $tmp_object->int_attr = 123; $memcache->set(''key'', $tmp_object, false, 10) or die ("Failed to save data at the server"); echo "Store data in the cache (data will expire in 10 seconds)n"; $get_result = $memcache->get(''key''); echo "Data from the cache:n"; var_dump($get_result); ?>
memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
以上就是本文的全部内容,希望对大家学习php缓存有所帮助。
今天关于ThinkPHP文件缓存类代码分享和thinkphp 缓存的分享就到这里,希望大家有所收获,若想了解更多关于php数据文件缓存类代码实例、php文件缓存类、php文件缓存类实例整理、PHP文件缓存类实现代码等相关知识,可以在本站进行查询。
本文标签: