123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- // +--------------------------------------------------------------------------
- // | Senthot [ DEVELOPED BY ME ]
- // +--------------------------------------------------------------------------
- // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
- // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
- // | Author: ms134n ( [email protected] )
- // +--------------------------------------------------------------------------
- defined('SEN_PATH') or exit();
- /**
- * File type of cache class
- * @category Sen
- * @package Sen
- * @subpackage Driver.Cache
- * @author ms134n <[email protected]>
- */
- class CacheFile extends Cache {
- /**
- * Architecture function
- * @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();
- }
- /**
- * Initialization check
- * @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.
- // Create a project cache directory
- if (!is_dir($this->options['temp'])) {
- if (! mkdir($this->options['temp']))
- return false;
- chmod($this->options['temp'], $dir_perms);
- }
- }
- /**
- * Retrieve variables to store the file name
- * @access private
- * @param string $name Cache variable name
- * @return string
- */
- private function filename($name) {
- $name = md5($name);
- if(C('DATA_CACHE_SUBDIR')) {
- // Using subdirectories
- $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;
- }
- /**
- * Read caching
- * @access public
- * @param string $name Cache variable 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) {
- //Delete the cache file cache expires
- unlink($filename);
- return false;
- }
- if(C('DATA_CACHE_CHECK')) {//Open data validation
- $check = substr($content,20, 32);
- $content = substr($content,52, -3);
- if($check != md5($content)) {//Parity error
- return false;
- }
- }else {
- $content = substr($content,20, -3);
- }
- if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
- //Enable data compression
- $content = gzuncompress($content);
- }
- $content = unserialize($content);
- return $content;
- }
- else {
- return false;
- }
- }
- /**
- * Write caching
- * @access public
- * @param string $name Cache variable name
- * @param mixed $value Stored data
- * @param int $expire Valid time 0 for permanent
- * @return boolen
- */
- 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 compression
- $data = gzcompress($data,3);
- }
- if(C('DATA_CACHE_CHECK')) {//Open data validation
- $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) {
- // Record cache queue
- $this->queue($name);
- }
- clearstatcache();
- return true;
- }else {
- return false;
- }
- }
- /**
- * Deleting the cache
- * @access public
- * @param string $name Cache variable name
- * @return boolen
- */
- public function rm($name) {
- return unlink($this->filename($name));
- }
- /**
- * Clearing the cache
- * @access public
- * @param string $name Cache variable 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;
- }
- }
- }
|