$w3tc_entry || '..' === $w3tc_entry ) { $w3tc_entry = @readdir( $dir ); continue; } $full_path = $path . DIRECTORY_SEPARATOR . $w3tc_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 ( 0 === $size['items'] % 1000 ) { $size['timeout_occurred'] |= ( time() > $timeout_time ); } } $w3tc_entry = @readdir( $dir ); } @closedir( $dir ); } return $size; } /** * Sets a new value if the old value matches the current value. * * This method checks if the current value in the cache matches the provided old value. If they match, it sets the new value. * Cannot guarantee atomicity due to potential file lock failures. * * @param string $w3tc_key Cache key. * @param mixed $old_value The expected current value. * @param mixed $new_value The value to set if the old value matches. * * @return bool True if the value was set, false otherwise. */ public function set_if_maybe_equals( $w3tc_key, $old_value, $new_value ) { // Cant guarantee atomic action here, filelocks fail often. $w3tc_value = $this->get( $w3tc_key ); if ( isset( $old_value['content'] ) && $w3tc_value['content'] !== $old_value['content'] ) { return false; } return $this->set( $w3tc_key, $new_value ); } /** * Increments a counter stored in the cache by a given value. * * This method appends the increment value to the counter file. If the value is 1, it stores it as 'x' for efficiency. Larger * increments are stored as space-separated integers. * * @param string $w3tc_key Cache key. * @param int $w3tc_value The increment value (must be non-zero). * * @return bool True on success, false on failure. */ public function counter_add( $w3tc_key, $w3tc_value ) { if ( 0 === $w3tc_value ) { return true; } $fp = $this->fopen_write( $w3tc_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 ( 1 === $w3tc_value ) { @fputs( $fp, 'x' ); } else { @fputs( $fp, ' ' . (int) $w3tc_value ); } @fclose( $fp ); return true; } /** * Sets a counter value in the cache. * * This method initializes a counter file with the provided value, along with an expiration time and a PHP exit directive to * prevent execution. * * @param string $w3tc_key Cache key. * @param int $w3tc_value The counter value to set. * * @return bool True on success, false on failure. */ public function counter_set( $w3tc_key, $w3tc_value ) { $fp = $this->fopen_write( $w3tc_key, '', 'wb' ); if ( ! $fp ) { return false; } $expire = W3TC_CACHE_FILE_EXPIRE_MAX; $expires_at = time() + $expire; @fputs( $fp, pack( 'L', $expires_at ) ); @fputs( $fp, '