[李景山php]thinkphp核心源码注释|File.class.php
来源:互联网 发布:淘宝店铺每日工作流程 编辑:程序博客网 时间:2024/06/02 12:15
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]// +----------------------------------------------------------------------// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------namespace Think\Cache\Driver;use Think\Cache;defined('THINK_PATH') or exit();/** * 文件类型缓存类 * 基础文件类型 */class File extends Cache { /** * 架构函数 * @access public * 初始化文件 引擎配置 */ public function __construct($options=array()) { if(!empty($options)) { $this->options = $options; } $this->options['temp'] = !empty($options['temp'])? $options['temp'] : C('DATA_CACHE_PATH'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; if(substr($this->options['temp'], -1) != '/') $this->options['temp'] .= '/'; $this->init(); } /** * 初始化检查 * @access private * @return boolean * 就是初始化创建目录而已 */ private function init() { // 创建应用缓存目录 if (!is_dir($this->options['temp'])) { mkdir($this->options['temp']); } } /** * 取得变量的存储文件名 * @access private * @param string $name 缓存变量名 * @return string * 就是根据变量名字 找到对应的文件名 */ private function filename($name) { $name=md5(C('DATA_CACHE_KEY').$name); if(C('DATA_CACHE_SUBDIR')) { // 使用子目录 $dir =''; for($i=0;$i<C('DATA_PATH_LEVEL');$i++) { $dir.=$name{$i}.'/'; } if(!is_dir($this->options['temp'].$dir)) { mkdir($this->options['temp'].$dir,0755,true); } $filename=$dir.$this->options['prefix'].$name.'.php'; }else{ $filename=$this->options['prefix'].$name.'.php'; } return $this->options['temp'].$filename; } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed * 获取 缓存数据 */ public function get($name) { $filename = $this->filename($name); if (!is_file($filename)) { return false; } N('cache_read',1);// 记录 $content = file_get_contents($filename);// 获取到内容 if( false !== $content) { $expire = (int)substr($content,8, 12);// 查看缓存是否过期 if($expire != 0 && time() > filemtime($filename) + $expire) { //缓存过期删除缓存文件 unlink($filename); return false; } if(C('DATA_CACHE_CHECK')) {//开启数据校验 $check = substr($content,20, 32); $content = substr($content,52, -3); if($check != md5($content)) {//校验错误 return false; } }else { $content = substr($content,20, -3); } if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //启用数据压缩 $content = gzuncompress($content); } $content = unserialize($content); return $content; } else { return false; } } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param int $expire 有效时间 0为永久 * @return boolean */ public function set($name,$value,$expire=null) { N('cache_write',1);// 设置读写次数 if(is_null($expire)) { $expire = $this->options['expire']; } $filename = $this->filename($name); // 此刻发现,这个就是 变化而已,升级版本而已 $data = serialize($value); if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) { //数据压缩 $data = gzcompress($data,3); } if(C('DATA_CACHE_CHECK')) {//开启数据校验 $check = md5($data); }else { $check = ''; } $data = "<?php\n//".sprintf('%012d',$expire).$check.$data."\n?>"; $result = file_put_contents($filename,$data); // 写入文件 if($result) { if($this->options['length']>0) { // 记录缓存队列 $this->queue($name); } clearstatcache(); return true; }else { return false; } } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean * 直接删掉文件 */ public function rm($name) { return unlink($this->filename($name)); } /** * 清除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function clear() { $path = $this->options['temp']; $files = scandir($path); // scandir() 函数返回一个数组,其中包含指定路径中的文件和目录。 // 神器啊 if($files){ // 批量删除啊。狠人啊 foreach($files as $file){ if ($file != '.' && $file != '..' && is_dir($path.$file) ){ array_map( 'unlink', glob( $path.$file.'/*.*' ) ); }elseif(is_file($path.$file)){ unlink( $path . $file ); } } return true; } return false; }}
0 0
- [李景山php]thinkphp核心源码注释|File.class.php
- [李景山php]thinkphp核心源码注释|File.class.php
- thinkphp核心源码注释|File.class.php
- [李景山php]thinkphp核心源码注释|View.class.php
- [李景山php]thinkphp核心源码注释|Think.class.php
- [李景山php]thinkphp核心源码注释|Template.class.php
- [李景山php]thinkphp核心源码注释|Storage.class.php
- [李景山php]thinkphp核心源码注释|Route.class.php
- [李景山php]thinkphp核心源码注释|Model.class.php
- [李景山php]thinkphp核心源码注释|Log.class.php
- [李景山php]thinkphp核心源码注释|Hook.class.php
- [李景山php]thinkphp核心源码注释|DB.class.php
- [李景山php]thinkphp核心源码注释|Controller.class.php
- [李景山php]thinkphp核心源码注释|Cache.class.php
- [李景山php]thinkphp核心源码注释|Build.class.php
- [李景山php]thinkphp核心源码注释|App.class.php
- [李景山php]thinkphp核心源码注释|Disaptcher.class.php
- [李景山php]thinkphp核心源码注释|Taglib.class.php
- as自写插件及butterknife
- 1073. Scientific Notation (20)
- Android ImageView设置边框
- Tricky Sum
- Vijos P1794 文化之旅
- [李景山php]thinkphp核心源码注释|File.class.php
- textView下划线
- v$session 视图中的 FAILOVER_TYPE 字段
- 如何在已有网站的服务器上面,通过nginx配置虚拟主机来达到多个应用使用同一个服务器的效果
- 在线生成myeclipse注册码
- 改进Scala实现贝叶斯算法
- [李景山php]thinkphp核心源码注释|Memcache.class.php
- 易语言特征码定位工具源码
- Mybatis DataSource Connection 相关源码