/sop/2.0beta1/ThinkPHP/Lib/Think/Util/Cache/CacheFile.class.php
PHP | 247 lines | 127 code | 12 blank | 108 comment | 29 complexity | 6b6a1434dedd3138edd16c1fbce26fa2 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // $Id$
- /**
- +------------------------------------------------------------------------------
- * ???????
- +------------------------------------------------------------------------------
- * @category Think
- * @package Think
- * @subpackage Util
- * @author liu21st <liu21st@gmail.com>
- * @version $Id$
- +------------------------------------------------------------------------------
- */
- class CacheFile extends Cache
- {//?????
- /**
- +----------------------------------------------------------
- * ????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- */
- public function __construct($options='')
- {
- if(!empty($options['temp'])){
- $this->options['temp'] = $options['temp'];
- }else {
- $this->options['temp'] = C('DATA_CACHE_PATH');
- }
- $this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
- if(substr($this->options['temp'], -1) != "/") $this->options['temp'] .= "/";
- $this->connected = is_dir($this->options['temp']) && is_writeable($this->options['temp']);
- $this->type = strtoupper(substr(__CLASS__,6));
- $this->init();
- }
- /**
- +----------------------------------------------------------
- * ?????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- private function init()
- {
- $stat = stat($this->options['temp']);
- $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
- $file_perms = $dir_perms & 0000666; // Remove execute bits for files.
- // ????????
- if (!is_dir($this->options['temp'])) {
- if (! mkdir($this->options['temp']))
- return false;
- chmod($this->options['temp'], $dir_perms);
- }
- }
- /**
- +----------------------------------------------------------
- * ????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- private function isConnected()
- {
- return $this->connected;
- }
- /**
- +----------------------------------------------------------
- * ??????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param string $name ?????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- private function filename($name)
- {
- $name = md5($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)) {
- mk_dir($this->options['temp'].$dir);
- }
- $filename = $dir.$this->prefix.$name.'.php';
- }else{
- $filename = $this->prefix.$name.'.php';
- }
- return $this->options['temp'].$filename;
- }
- /**
- +----------------------------------------------------------
- * ????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $name ?????
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- public function get($name)
- {
- $filename = $this->filename($name);
- if (!$this->isConnected() || !is_file($filename)) {
- return false;
- }
- $this->Q(1);
- $content = file_get_contents($filename);
- if( false !== $content) {
- $expire = (int)substr($content,8, 12);
- if($expire != -1 && 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 ???? -1 ???
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- public function set($name,$value,$expire='')
- {
- $this->W(1);
- if('' === $expire) {
- $expire = $this->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) {
- clearstatcache();
- return true;
- }else {
- return false;
- }
- }
- /**
- +----------------------------------------------------------
- * ????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $name ?????
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- public function rm($name)
- {
- return unlink($this->filename($name));
- }
- /**
- +----------------------------------------------------------
- * ????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $name ?????
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- public function clear()
- {
- $path = $this->options['temp'];
- if ( $dir = opendir( $path ) )
- {
- while ( $file = readdir( $dir ) )
- {
- $check = is_dir( $file );
- if ( !$check )
- unlink( $path . $file );
- }
- closedir( $dir );
- return true;
- }
- }
- }//?????
- ?>