Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving wipe/clear/purge routines + QC back compat. See: websharks/quick-cache#397 #13

Merged
merged 3 commits into from
Jan 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions zencache-pro/includes/advanced-cache.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,8 @@ public function output_buffer_callback_handler($buffer, $phase)

# Exclusion checks; there are MANY of these...

advanced_cache_back_compat::QUICK_CACHE_constants();

$cache = trim((string)$buffer);
if(!isset($cache[0])) // Allows a `0`.
return FALSE; // Don't cache an empty buffer.
Expand Down Expand Up @@ -1619,6 +1621,9 @@ public static function QUICK_CACHE_constants()
define('ZENCACHE_'.$_constant_sub_name, $_value);
}
unset($_constant, $_value); // Housekeeping.

if(isset($_SERVER['QUICK_CACHE_ALLOWED']) && !isset($_SERVER['ZENCACHE_ALLOWED']))
$_SERVER['ZENCACHE_ALLOWED'] = $_SERVER['QUICK_CACHE_ALLOWED'];
}
}

Expand Down
189 changes: 136 additions & 53 deletions zencache-pro/includes/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -1541,32 +1541,63 @@ public function delete_files_from_cache_dir($regex, $check_max_age = FALSE)
if(!rename($cache_dir, $cache_dir_tmp)) // Work from tmp directory so deletions are atomic.
throw new \exception(sprintf(__('Unable to delete files. Rename failure on directory: `%1$s`.', $this->text_domain), $cache_dir));

/** @var $_file_dir \RecursiveDirectoryIterator Regex iterator reference for IDEs. */
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($cache_dir_tmp, $cache_dir_tmp_regex)) as $_file_dir)
/** @var $_resource \RecursiveDirectoryIterator Regex iterator reference for IDEs. */
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($cache_dir_tmp, $cache_dir_tmp_regex)) as $_resource)
{
if(($_file_dir->isFile() || $_file_dir->isLink()) // Files and/or symlinks only.
$_resource_type = $_resource->getType();
$_sub_path_name = $_resource->getSubpathname();
$_path_name = $_resource->getPathname();

// Don't delete files in the immediate directory; e.g. `zc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache files are nested. Files in the immediate directory are for other purposes.
&& (strpos($_file_dir->getSubpathname(), '/') !== FALSE)
if($_resource_type !== 'dir' && strpos($_sub_path_name, '/') === FALSE)
continue; // Don't delete links/files in the immediate directory; e.g. `zc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache links/files are nested. Links/files in the immediate directory are for other purposes.

// If NOT checking max age; or if we ARE, and the file has expired now.
&& (!$check_max_age || (!empty($max_age) && $_file_dir->getMTime() < $max_age))

) // Throw an exception if a deletion failure occurs.
{
if(!unlink($_file_dir->getPathname())) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
$counter++; // Increment counter for each file we delete.
}
else if(!$check_max_age && $regex === '/^.+/i' && $_file_dir->isDir()) // Directories too?
switch($_resource_type) // Based on type; i.e. `link`, `file`, `dir`.
{
if(!rmdir($_file_dir->getPathname())) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
# $counter++; // Increment counter for each directory we delete. ~ NO don't do that here.
case 'link': // Symbolic links; i.e. 404 errors.

if($check_max_age && !empty($max_age) && is_file($_resource->getLinkTarget()))
if(($_lstat = lstat($_path_name)) && !empty($_lstat['mtime']))
if($_lstat['mtime'] >= $max_age) // Still valid?
break; // Break switch handler.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete symlink: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each link we delete.

break; // Break switch handler.

case 'file': // Regular files; i.e. not symlinks.

if($check_max_age && !empty($max_age)) // Should check max age?
if($_resource->getMTime() >= $max_age) // Still valid?
break; // Break switch handler.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each file we delete.

break; // Break switch handler.

case 'dir': // A regular directory; i.e. not a symlink.

if($regex !== '/^.+/i') // Deleting everything?
break; // Break switch handler. Not deleting everything.

if($check_max_age && !empty($max_age)) // Should check max age?
break; // Break switch handler. Not deleting everything in this case.

if(!rmdir($_path_name)) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_path_name));
# $counter++; // Increment counter for each directory we delete. ~ NO don't do that here.

break; // Break switch handler.

default: // Something else that is totally unexpected here.
throw new \exception(sprintf(__('Unexpected resource type: `%1$s`.', $this->text_domain), $_resource_type));
}
}
unset($_dir_regex_iteration, $_file_dir); // Housekeeping after this `foreach()` loop.
unset($_dir_regex_iteration, $_resource, $_resource_type, $_sub_path_name, $_path_name, $_lstat); // Housekeeping.

if(!rename($cache_dir_tmp, $cache_dir)) // Deletions are atomic; restore original directory now.
throw new \exception(sprintf(__('Unable to delete files. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $cache_dir_tmp));
Expand Down Expand Up @@ -1672,31 +1703,62 @@ public function delete_files_from_host_cache_dir($regex, $check_max_age = FALSE)
throw new \exception(sprintf(__('Unable to delete files. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $_host_cache_dir));

/** @var $_file_dir \RecursiveDirectoryIterator Regex iterator reference for IDEs. */
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($_host_cache_dir_tmp, $_host_cache_dir_tmp_regex)) as $_file_dir)
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($_host_cache_dir_tmp, $_host_cache_dir_tmp_regex)) as $_resource)
{
if(($_file_dir->isFile() || $_file_dir->isLink()) // Files and/or symlinks only.

// Don't delete files in the immediate directory; e.g. `zc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache files are nested. Files in the immediate directory are for other purposes.
&& ($_host_cache_dir !== $cache_dir || strpos($_file_dir->getSubpathname(), '/') !== FALSE)
$_resource_type = $_resource->getType();
$_sub_path_name = $_resource->getSubpathname();
$_path_name = $_resource->getPathname();

// If NOT checking max age; or if we ARE, and the file has expired now.
&& (!$check_max_age || (!empty($max_age) && $_file_dir->getMTime() < $max_age))
if($_host_cache_dir === $cache_dir && $_resource_type !== 'dir' && strpos($_sub_path_name, '/') === FALSE)
continue; // Don't delete links/files in the immediate directory; e.g. `zc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache links/files are nested. Links/files in the immediate directory are for other purposes.

) // Throw an exception if a deletion failure occurs.
switch($_resource_type) // Based on type; i.e. `link`, `file`, `dir`.
{
if(!unlink($_file_dir->getPathname())) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
$counter++; // Increment counter for each file we delete.
}
else if(!$check_max_age && $regex === '/^.+/i' && $_file_dir->isDir()) // Directories too?
{
if(!rmdir($_file_dir->getPathname())) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
# $counter++; // Increment counter for each directory we delete. ~ NO don't do that here.
case 'link': // Symbolic links; i.e. 404 errors.

if($check_max_age && !empty($max_age) && is_file($_resource->getLinkTarget()))
if(($_lstat = lstat($_path_name)) && !empty($_lstat['mtime']))
if($_lstat['mtime'] >= $max_age) // Still valid?
break; // Break switch handler.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete symlink: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each link we delete.

break; // Break switch handler.

case 'file': // Regular files; i.e. not symlinks.

if($check_max_age && !empty($max_age)) // Should check max age?
if($_resource->getMTime() >= $max_age) // Still valid?
break; // Break switch handler.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each file we delete.

break; // Break switch handler.

case 'dir': // A regular directory; i.e. not a symlink.

if($regex !== '/^.+/i') // Deleting everything?
break; // Break switch handler. Not deleting everything.

if($check_max_age && !empty($max_age)) // Should check max age?
break; // Break switch handler. Not deleting everything in this case.

if(!rmdir($_path_name)) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_path_name));
# $counter++; // Increment counter for each directory we delete. ~ NO don't do that here.

break; // Break switch handler.

default: // Something else that is totally unexpected here.
throw new \exception(sprintf(__('Unexpected resource type: `%1$s`.', $this->text_domain), $_resource_type));
}
}
unset($_dir_regex_iteration, $_file_dir); // Housekeeping after this `foreach()` loop.
unset($_dir_regex_iteration, $_resource, $_resource_type, $_sub_path_name, $_path_name, $_lstat); // Housekeeping.

if(!rename($_host_cache_dir_tmp, $_host_cache_dir)) // Deletions are atomic; restore original directory now.
throw new \exception(sprintf(__('Unable to delete files. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $_host_cache_dir_tmp));
Expand Down Expand Up @@ -1756,22 +1818,43 @@ public function delete_all_files_dirs_in($dir, $delete_dir_too = FALSE)
throw new \exception(sprintf(__('Unable to delete all files/dirs. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $dir));

/** @var $_file_dir \RecursiveDirectoryIterator for IDEs. */
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($dir_temp, '/.+/')) as $_file_dir)
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($dir_temp, '/.+/')) as $_resource)
{
if(($_file_dir->isFile() || $_file_dir->isLink())) // Files and/or symlinks.
{
if(!unlink($_file_dir->getPathname())) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
$counter++; // Increment counter for each file we delete.
}
else if($_file_dir->isDir()) // Directories are last in the iteration; it should be empty now.
$_resource_type = $_resource->getType();
$_sub_path_name = $_resource->getSubpathname();
$_path_name = $_resource->getPathname();

switch($_resource_type) // Based on type; i.e. `link`, `file`, `dir`.
{
if(!rmdir($_file_dir->getPathname())) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
$counter++; // Increment counter for each directory we delete.
case 'link': // Symbolic links; i.e. 404 errors.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete symlink: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each link we delete.

break; // Break switch handler.

case 'file': // Regular files; i.e. not symlinks.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each file we delete.

break; // Break switch handler.

case 'dir': // A regular directory; i.e. not a symlink.

if(!rmdir($_path_name)) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each directory we delete.

break; // Break switch handler.

default: // Something else that is totally unexpected here.
throw new \exception(sprintf(__('Unexpected resource type: `%1$s`.', $this->text_domain), $_resource_type));
}
}
unset($_dir_regex_iteration, $_file_dir); // Housekeeping after this `foreach()` loop.
unset($_dir_regex_iteration, $_resource, $_resource_type, $_sub_path_name, $_path_name); // Housekeeping.

if(!rename($dir_temp, $dir)) // Deletions are atomic; restore original directory now.
throw new \exception(sprintf(__('Unable to delete all files/dirs. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $dir_temp));
Expand Down Expand Up @@ -1811,7 +1894,7 @@ public function delete_all_files_dirs_in($dir, $delete_dir_too = FALSE)
public function cache_lock()
{
if($this->apply_wp_filters(__CLASS__.'_disable_cache_locking', FALSE))
return false;
return FALSE;

if(!($wp_config_file = $this->find_wp_config_file()))
throw new \exception(__('Unable to find the wp-config.php file.', $this->text_domain));
Expand All @@ -1833,7 +1916,7 @@ public function cache_lock()
throw new \exception(__('No writable tmp directory.', $this->text_domain));

$inode_key = fileinode($wp_config_file);
$mutex = $tmp_dir.'/'.$this->slug.'-'.$inode_key.'.lock';
$mutex = $tmp_dir.'/'.$this->slug.'-'.$inode_key.'.lock';
if(!($resource = fopen($mutex, 'w')) || !flock($resource, LOCK_EX))
throw new \exception(__('Unable to obtain an exclusive lock.', $this->text_domain));

Expand Down