CacheFile.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. defined('SEN_PATH') or exit();
  10. /**
  11. * File type of cache class
  12. * @category Sen
  13. * @package Sen
  14. * @subpackage Driver.Cache
  15. * @author ms134n <[email protected]>
  16. */
  17. class CacheFile extends Cache {
  18. /**
  19. * Architecture function
  20. * @access public
  21. */
  22. public function __construct($options=array()) {
  23. if(!empty($options)) {
  24. $this->options = $options;
  25. }
  26. $this->options['temp'] = !empty($options['temp'])? $options['temp'] : C('DATA_CACHE_PATH');
  27. $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
  28. $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
  29. $this->options['length'] = isset($options['length'])? $options['length'] : 0;
  30. if(substr($this->options['temp'], -1) != '/') $this->options['temp'] .= '/';
  31. $this->init();
  32. }
  33. /**
  34. * Initialization check
  35. * @access private
  36. * @return boolen
  37. */
  38. private function init() {
  39. $stat = stat($this->options['temp']);
  40. $dir_perms = $stat['mode'] & 0007777; // Get the permission bits.
  41. $file_perms = $dir_perms & 0000666; // Remove execute bits for files.
  42. // Create a project cache directory
  43. if (!is_dir($this->options['temp'])) {
  44. if (! mkdir($this->options['temp']))
  45. return false;
  46. chmod($this->options['temp'], $dir_perms);
  47. }
  48. }
  49. /**
  50. * Retrieve variables to store the file name
  51. * @access private
  52. * @param string $name Cache variable name
  53. * @return string
  54. */
  55. private function filename($name) {
  56. $name = md5($name);
  57. if(C('DATA_CACHE_SUBDIR')) {
  58. // Using subdirectories
  59. $dir ='';
  60. for($i=0;$i<C('DATA_PATH_LEVEL');$i++) {
  61. $dir .= $name{$i}.'/';
  62. }
  63. if(!is_dir($this->options['temp'].$dir)) {
  64. mkdir($this->options['temp'].$dir,0755,true);
  65. }
  66. $filename = $dir.$this->options['prefix'].$name.'.php';
  67. }else{
  68. $filename = $this->options['prefix'].$name.'.php';
  69. }
  70. return $this->options['temp'].$filename;
  71. }
  72. /**
  73. * Read caching
  74. * @access public
  75. * @param string $name Cache variable name
  76. * @return mixed
  77. */
  78. public function get($name) {
  79. $filename = $this->filename($name);
  80. if (!is_file($filename)) {
  81. return false;
  82. }
  83. N('cache_read',1);
  84. $content = file_get_contents($filename);
  85. if( false !== $content) {
  86. $expire = (int)substr($content,8, 12);
  87. if($expire != 0 && time() > filemtime($filename) + $expire) {
  88. //Delete the cache file cache expires
  89. unlink($filename);
  90. return false;
  91. }
  92. if(C('DATA_CACHE_CHECK')) {//Open data validation
  93. $check = substr($content,20, 32);
  94. $content = substr($content,52, -3);
  95. if($check != md5($content)) {//Parity error
  96. return false;
  97. }
  98. }else {
  99. $content = substr($content,20, -3);
  100. }
  101. if(C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  102. //Enable data compression
  103. $content = gzuncompress($content);
  104. }
  105. $content = unserialize($content);
  106. return $content;
  107. }
  108. else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * Write caching
  114. * @access public
  115. * @param string $name Cache variable name
  116. * @param mixed $value Stored data
  117. * @param int $expire Valid time 0 for permanent
  118. * @return boolen
  119. */
  120. public function set($name,$value,$expire=null) {
  121. N('cache_write',1);
  122. if(is_null($expire)) {
  123. $expire = $this->options['expire'];
  124. }
  125. $filename = $this->filename($name);
  126. $data = serialize($value);
  127. if( C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
  128. //Data compression
  129. $data = gzcompress($data,3);
  130. }
  131. if(C('DATA_CACHE_CHECK')) {//Open data validation
  132. $check = md5($data);
  133. }else {
  134. $check = '';
  135. }
  136. $data = "<?php\n//".sprintf('%012d',$expire).$check.$data."\n?>";
  137. $result = file_put_contents($filename,$data);
  138. if($result) {
  139. if($this->options['length']>0) {
  140. // Record cache queue
  141. $this->queue($name);
  142. }
  143. clearstatcache();
  144. return true;
  145. }else {
  146. return false;
  147. }
  148. }
  149. /**
  150. * Deleting the cache
  151. * @access public
  152. * @param string $name Cache variable name
  153. * @return boolen
  154. */
  155. public function rm($name) {
  156. return unlink($this->filename($name));
  157. }
  158. /**
  159. * Clearing the cache
  160. * @access public
  161. * @param string $name Cache variable name
  162. * @return boolen
  163. */
  164. public function clear() {
  165. $path = $this->options['temp'];
  166. if ( $dir = opendir( $path ) ) {
  167. while ( $file = readdir( $dir ) ) {
  168. $check = is_dir( $file );
  169. if ( !$check )
  170. unlink( $path . $file );
  171. }
  172. closedir( $dir );
  173. return true;
  174. }
  175. }
  176. }