diff --git a/CHANGELOG.md b/CHANGELOG.md index 43255e7..c9b7fa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # BC Cache Changelog +## Version 3.3.1 (2024-04-18) + +### Fixed + +* When deleting cache item (URL + request variant), corresponding `.htaccess` file and cache entry directory is only deleted if there are no other cache items (ie. other request variants for given URL) saved in the cache entry directory [#109](https://github.com/chesio/bc-cache/issues/109). + ## Version 3.3.0 (2024-04-04) PHP 8.1 and WordPress 6.2 are now required. diff --git a/bc-cache.php b/bc-cache.php index 64929b5..8952f23 100644 --- a/bc-cache.php +++ b/bc-cache.php @@ -4,7 +4,7 @@ * Plugin Name: BC Cache * Plugin URI: https://github.com/chesio/bc-cache * Description: Simple full page cache plugin inspired by Cachify. - * Version: 3.3.0 + * Version: 3.3.1 * Author: Česlav Przywara * Author URI: https://www.chesio.com * Requires PHP: 8.1 diff --git a/classes/BlueChip/Cache/Core.php b/classes/BlueChip/Cache/Core.php index b8a5b97..01be13c 100644 --- a/classes/BlueChip/Cache/Core.php +++ b/classes/BlueChip/Cache/Core.php @@ -78,7 +78,7 @@ public function tearDown(): bool /** - * @return string[] Filtered list of request variants. + * @return array Filtered list of request variants. */ public function getRequestVariants(): array { @@ -170,13 +170,22 @@ public function delete(Item $item): bool return false; } + // There can be more request variants cached for this URL, so we have to treat carefully. + $request_variant = $item->getRequestVariant(); + $other_request_variants = \array_diff(\array_keys($this->getRequestVariants()), [$request_variant]); + try { + // Attempt to delete cache files. $bytes_deleted - = self::deleteFile(self::getPlainFilename($path, $item->getRequestVariant())) - + self::deleteFile(self::getGzipFilename($path, $item->getRequestVariant())) - + self::deleteFile(self::getHtaccessFilename($path)) + = self::deleteFile(self::getPlainFilename($path, $request_variant)) + + self::deleteFile(self::getGzipFilename($path, $request_variant)) ; - \rmdir($path); + // If there are no other request variants defined or they have no entries for this URL... + if (($other_request_variants === []) || (self::getCacheEntrySizes($path, $other_request_variants) === [])) { + // ...proceed to delete .htaccess file and the whole directory. + $bytes_deleted += self::deleteFile(self::getHtaccessFilename($path)); + \rmdir($path); + } // Update cache size. $this->cache_info->decrementSize($bytes_deleted); // :) @@ -228,16 +237,24 @@ public function push(Item $item, array $headers, string $data): bool return false; } + // There can be more request variants cached for this URL, so we have to treat carefully. + $request_variant = $item->getRequestVariant(); + $other_request_variants = \array_diff(\array_keys($this->getRequestVariants()), [$request_variant]); + try { // Write cache date to disk, get number of bytes written. - $bytes_written = self::writeFile(self::getPlainFilename($path, $item->getRequestVariant()), $data); + $bytes_written = self::writeFile(self::getPlainFilename($path, $request_variant), $data); if (($gzip = \gzencode($data, 9)) !== false) { - $bytes_written += self::writeFile(self::getGzipFilename($path, $item->getRequestVariant()), $gzip); + $bytes_written += self::writeFile(self::getGzipFilename($path, $request_variant), $gzip); + } + // If there are no other request variants defined or they have no entries for this URL... + if (($other_request_variants === []) || (self::getCacheEntrySizes($path, $other_request_variants) === [])) { + // ...proceed to create new .htaccess file. + $bytes_written += self::writeFile( + self::getHtaccessFilename($path), + self::prepareHtaccessFile($headers) + ); } - $bytes_written += self::writeFile( - self::getHtaccessFilename($path), - self::prepareHtaccessFile($headers) - ); // Increment cache size. $this->cache_info->incrementSize($bytes_written); // :)