Skip to content

Commit

Permalink
Merge branch 'hotfix-3.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
chesio committed Apr 18, 2024
2 parents 033395c + 57a9ba1 commit 7152b41
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bc-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ceslav@przywara.cz>
* Author URI: https://www.chesio.com
* Requires PHP: 8.1
Expand Down
39 changes: 28 additions & 11 deletions classes/BlueChip/Cache/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function tearDown(): bool


/**
* @return string[] Filtered list of request variants.
* @return array<string,string> Filtered list of request variants.
*/
public function getRequestVariants(): array
{
Expand Down Expand Up @@ -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);
// :)
Expand Down Expand Up @@ -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);
// :)
Expand Down

0 comments on commit 7152b41

Please sign in to comment.