keycdn / cache-enabler

A lightweight caching plugin for WordPress that makes your website faster by generating static HTML files.
https://wordpress.org/plugins/cache-enabler/
123 stars 46 forks source link

add wildcard cache clearing #246

Closed coreykn closed 3 years ago

coreykn commented 3 years ago

Add support for clearing the cache by URL with a trailing asterisk (*) that will match zero or more characters. This is one of two pull requests that are coming after the support for this was added to the directory object filter in PR #245. (The next PR will be using this new feature to purge the comments pagination.) For example, clearing https://www.example.com/blog/how-to-* will now clear the cache for any page that the URL contains www.example.com/blog/how-to-. Clearing https://www.example.com/blog/* will do the same, but will also clear the page https://www.example.com/blog/ itself, unlike the previous example. Using a query string to pass arguments is still supported, so https://www.example.com/blog/how-to-*?expired=1 will still clear all matching pages that are expired. (Using a subpages inclusion in the query string would overwrite the wildcard in the URL path because query string parameters take precedence. It could also cause unwanted results due to the root argument that will be automatically set. That means using that query parameter when clearing the cache with a wildcard URL is not recommended.)

root is a new cache iterator argument being introduced, extending what was added in PR #237. This argument allows the wildcard matching to be successful by defining a root directory path, which can be partial or full, to optionally be set. The purpose of this is to only iterate over cache objects (files and directories) that a part of the given path (likely only when a wildcard path is provided). For example, in the wildcard examples above, the root directory path that would automatically be set would be /path/to/wp-content/cache/cache-enabler/www.example.com/blog/how-to- and /path/to/wp-content/cache/cache-enabler/www.example.com/blog/. In the case of the former path, it would prevent the root cache files from being iterated over (e.g. /path/to/wp-content/cache/cache-enabler/www.example.com/blog/https-index.html).

This new argument was required due to how the cache iterator works. A URL is given to it and if a directory matching that URL is found we can iterate through the cache objects. In order to find pages that match https://www.example.com/blog/how-to-* we have to query https://www.example.com/blog/ to search for subpages that match how-to-*. While we can choose to include/exclude subpages (directories) and include/exclude cache keys (which is applied to all files), it was not considered that it would need to exclude the given page itself. The root argument allows the "starting point" to be extended, allowing the original root path to be overwritten.