Cache_File.php 10.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
<?php
namespace W3TC;

/**
 * class Cache_File
 */
class Cache_File extends Cache_Base {
	/**
	 * Path to cache dir
	 *
	 * @var string
	 */
	protected $_cache_dir = '';

	/**
	 * Directory to flush
	 *
	 * @var string
	 */
	protected $_flush_dir = '';
	/**
	 * Exclude files
	 *
	 * @var array
	 */
	protected $_exclude = array();

	/**
	 * Flush time limit
	 *
	 * @var int
	 */
	protected $_flush_timelimit = 0;

	/**
	 * File locking
	 *
	 * @var boolean
	 */
	protected $_locking = false;

	/**
	 * If path should be generated based on wp_hash
	 *
	 * @var bool
	 */
	protected $_use_wp_hash = false;

	/**
	 * Constructor
	 *
	 * @param array   $config
	 */
	function __construct( $config = array() ) {
		parent::__construct( $config );
		if ( isset( $config['cache_dir'] ) )
			$this->_cache_dir = trim( $config['cache_dir'] );
		else
			$this->_cache_dir = Util_Environment::cache_blog_dir( $config['section'], $config['blog_id'] );

		$this->_exclude = isset( $config['exclude'] ) ? (array) $config['exclude'] : array();
		$this->_flush_timelimit = isset( $config['flush_timelimit'] ) ? (int) $config['flush_timelimit'] : 180;
		$this->_locking = isset( $config['locking'] ) ? (boolean) $config['locking'] : false;

		if ( isset( $config['flush_dir'] ) )
			$this->_flush_dir = $config['flush_dir'];
		else {
			if ( $config['blog_id'] <= 0 && !isset( $config['cache_dir'] ) ) {
				// clear whole section if we operate on master cache
				// and in a mode when cache_dir not strictly specified
				$this->_flush_dir = Util_Environment::cache_dir( $config['section'] );
			} else
				$this->_flush_dir = $this->_cache_dir;
		}
		if ( isset( $config['use_wp_hash'] ) && $config['use_wp_hash'] )
			$this->_use_wp_hash = true;
	}

	/**
	 * Adds data
	 *
	 * @param string  $key
	 * @param mixed   $var
	 * @param integer $expire
	 * @param string  $group  Used to differentiate between groups of cache values
	 * @return boolean
	 */
	function add( $key, &$var, $expire = 0, $group = '' ) {
		if ( $this->get( $key, $group ) === false ) {
			return $this->set( $key, $var, $expire, $group );
		}

		return false;
	}

	/**
	 * Sets data
	 *
	 * @param string  $key
	 * @param mixed   $var
	 * @param integer $expire
	 * @param string  $group  Used to differentiate between groups of cache values
	 * @return boolean
	 */
	function set( $key, $var, $expire = 0, $group = '' ) {
		$fp = $this->fopen_write( $key, $group, 'wb' );
		if ( !$fp )
			return false;

		if ( $this->_locking )
			@flock( $fp, LOCK_EX );

		if ( $expire <= 0 || $expire > W3TC_CACHE_FILE_EXPIRE_MAX )
			$expire = W3TC_CACHE_FILE_EXPIRE_MAX;

		$expires_at = time() + $expire;
		@fputs( $fp, pack( 'L', $expires_at ) );
		@fputs( $fp, '<?php exit; ?>' );
		@fputs( $fp, @serialize( $var ) );
		@fclose( $fp );

		if ( $this->_locking )
			@flock( $fp, LOCK_UN );

		return true;
	}

	/**
	 * Returns data
	 *
	 * @param string  $key
	 * @param string  $group Used to differentiate between groups of cache values
	 * @return mixed
	 */
	function get_with_old( $key, $group = '' ) {
		list( $data, $has_old_data ) = $this->_get_with_old_raw( $key, $group );
		if ( !empty( $data ) )
			$data_unserialized = @unserialize( $data );
		else
			$data_unserialized = $data;

		return array( $data_unserialized, $has_old_data );
	}



	private function _get_with_old_raw( $key, $group = '' ) {
		$has_old_data = false;

		$storage_key = $this->get_item_key( $key );

		$path = $this->_cache_dir . DIRECTORY_SEPARATOR .
			( $group ? $group . DIRECTORY_SEPARATOR : '' ) .
			$this->_get_path( $storage_key );
		if ( !is_readable( $path ) )
			return array( null, $has_old_data );

		$fp = @fopen( $path, 'rb' );
		if ( !$fp )
			return array( null, $has_old_data );

		if ( $this->_locking )
			@flock( $fp, LOCK_SH );

		$expires_at = @fread( $fp, 4 );
		$data = null;

		if ( $expires_at !== false ) {
			list( , $expires_at ) = @unpack( 'L', $expires_at );

			if ( time() > $expires_at ) {
				if ( $this->_use_expired_data ) {
					// update expiration so other threads will use old data
					$fp2 = @fopen( $path, 'cb' );

					if ( $fp2 ) {
						@fputs( $fp2, pack( 'L', time() + 30 ) );
						@fclose( $fp2 );
					}
					$has_old_data = true;
				}
			} else {
				$data = '';

				while ( !@feof( $fp ) ) {
					$data .= @fread( $fp, 4096 );
				}
				$data = substr( $data, 14 );
			}

		}

		if ( $this->_locking )
			@flock( $fp, LOCK_UN );

		@fclose( $fp );

		return array( $data, $has_old_data );
	}

	/**
	 * Replaces data
	 *
	 * @param string  $key
	 * @param mixed   $var
	 * @param integer $expire
	 * @param string  $group  Used to differentiate between groups of cache values
	 * @return boolean
	 */
	function replace( $key, &$var, $expire = 0, $group = '' ) {
		if ( $this->get( $key, $group ) !== false ) {
			return $this->set( $key, $var, $expire, $group );
		}

		return false;
	}

	/**
	 * Deletes data
	 *
	 * @param string  $key
	 * @param string  $group Used to differentiate between groups of cache values
	 * @return boolean
	 */
	function delete( $key, $group = '' ) {
		$storage_key = $this->get_item_key( $key );

		$path = $this->_cache_dir . DIRECTORY_SEPARATOR .
			( $group ? $group . DIRECTORY_SEPARATOR : '' ) .
			$this->_get_path( $storage_key );

		if ( !file_exists( $path ) )
			return true;

		if ( $this->_use_expired_data ) {
			$fp = @fopen( $path, 'cb' );

			if ( $fp ) {
				if ( $this->_locking )
					@flock( $fp, LOCK_EX );

				@fputs( $fp, pack( 'L', 0 ) );   // make it expired
				@fclose( $fp );

				if ( $this->_locking )
					@flock( $fp, LOCK_UN );
				return true;
			}

		}

		return @unlink( $path );
	}

	/**
	 * Key to delete, deletes .old and primary if exists.
	 *
	 * @param string  $key
	 *
	 * @return bool
	 */
	function hard_delete( $key ) {
		$key = $this->get_item_key( $key );
		$path = $this->_cache_dir . DIRECTORY_SEPARATOR . $this->_get_path( $key );
		return @unlink( $path );
	}

	/**
	 * Flushes all data
	 *
	 * @param string  $group Used to differentiate between groups of cache values
	 * @return boolean
	 */
	function flush( $group = '' ) {
		@set_time_limit( $this->_flush_timelimit );
		$flush_dir = $group ?
			$this->_cache_dir . DIRECTORY_SEPARATOR . $group .
			DIRECTORY_SEPARATOR :
			$this->_flush_dir;
		Util_File::emptydir( $flush_dir, $this->_exclude );
		return true;
	}

	/**
	 * Returns modification time of cache file
	 *
	 * @param integer $key
	 * @param string  $group Used to differentiate between groups of cache values
	 * @return boolean|string
	 */
	function mtime( $key, $group = '' ) {
		$path =
			$this->_cache_dir . DIRECTORY_SEPARATOR .
			( $group ? $group . DIRECTORY_SEPARATOR : '' ) .
			$this->_get_path( $key );

		if ( file_exists( $path ) ) {
			return @filemtime( $path );
		}

		return false;
	}

	/**
	 * Returns file path for key
	 *
	 * @param string  $key
	 * @return string
	 */
	function _get_path( $key ) {
		if ( $this->_use_wp_hash && function_exists( 'wp_hash' ) )
			$hash = wp_hash( $key );
		else
			$hash = md5( $key );

		$path = sprintf( '%s/%s/%s.php', substr( $hash, 0, 3 ), substr( $hash, 3, 3 ), $hash );

		return $path;
	}

	public function get_stats_size( $timeout_time ) {
		$size = array(
			'bytes' => 0,
			'items' => 0,
			'timeout_occurred' => false
		);

		$size = $this->dirsize( $this->_cache_dir, $size, $timeout_time );
		return $size;
	}



	private function dirsize( $path, $size, $timeout_time ) {
		$dir = @opendir( $path );

		if ( $dir ) {
			while ( !$size['timeout_occurred'] && ( $entry = @readdir( $dir ) ) !== false ) {
				if ( $entry == '.' || $entry == '..' ) {
					continue;
				}

				$full_path = $path . DIRECTORY_SEPARATOR . $entry;

				if ( @is_dir( $full_path ) ) {
					$size = $this->dirsize( $full_path, $size, $timeout_time );
				} else {
					$size['bytes'] += @filesize( $full_path );

					// dont check time() for each file, quite expensive
					$size['items']++;
					if ( $size['items'] % 1000 == 0 )
						$size['timeout_occurred'] |= ( time() > $timeout_time );
				}
			}

			@closedir( $dir );
		}

		return $size;
	}

	/**
	 * Used to replace as atomically as possible known value to new one
	 */
	public function set_if_maybe_equals( $key, $old_value, $new_value ) {
		// cant guarantee atomic action here, filelocks fail often
		$value = $this->get( $key );
		if ( isset( $old_value['content'] ) &&
			$value['content'] != $old_value['content'] )
			return false;

		return $this->set( $key, $new_value );
	}

	/**
	 * Use key as a counter and add integet value to it
	 */
	public function counter_add( $key, $value ) {
		if ( $value == 0 )
			return true;

		$fp = $this->fopen_write( $key, '', 'a' );
		if ( !$fp )
			return false;

		// use "x" to store increment, since it's most often case
		// and it will save 50% of size if only increments are used
		if ( $value == 1 )
			@fputs( $fp, 'x' );
		else
			@fputs( $fp, ' ' . (int)$value );

		@fclose( $fp );
		return true;
	}

	/**
	 * Use key as a counter and add integet value to it
	 */
	public function counter_set( $key, $value ) {
		$fp = $this->fopen_write( $key, '', 'wb' );
		if ( !$fp )
			return false;

		$expire = W3TC_CACHE_FILE_EXPIRE_MAX;
		$expires_at = time() + $expire;

		@fputs( $fp, pack( 'L', $expires_at ) );
		@fputs( $fp, '<?php exit; ?>' );
		@fputs( $fp, (int)$value );
		@fclose( $fp );

		return true;
	}

	/**
	 * Get counter's value
	 */
	public function counter_get( $key ) {
		list( $value, $old_data ) = $this->_get_with_old_raw( $key );
		if ( empty( $value ) )
			return 0;

		$original_length = strlen( $value );
		$cut_value = str_replace( 'x', '', $value );

		$count = $original_length - strlen( $cut_value );

		// values more than 1 are stored as <space>value
		$a = explode( ' ', $cut_value );
		foreach ( $a as $counter_value )
			$count += (int)$counter_value;

		return $count;
	}

	private function fopen_write( $key, $group, $mode ) {
		$storage_key = $this->get_item_key( $key );

		$sub_path = $this->_get_path( $storage_key );
		$path = $this->_cache_dir . DIRECTORY_SEPARATOR .
			( $group ? $group . DIRECTORY_SEPARATOR : '' ) . $sub_path;

		$dir = dirname( $path );

		if ( !@is_dir( $dir ) ) {
			if ( !Util_File::mkdir_from( $dir, W3TC_CACHE_DIR ) )
				return false;
		}

		$fp = @fopen( $path, $mode );
		return $fp;
	}
}