The Debounced Cache Clearer plugin for Payload CMS 3.0 automatically clears cache for specified collections after a defined debounce time. Additionally, it offers the ability to manually trigger cache deletion.
- Automatic Cache Clearing: Clears cache after a specified debounce time following any changes (update, or delete) to specified collections. The debounce time can be customized.
- Manual Cache Deletion: Provides a UI button in the admin panel for manually triggering cache deletion.
- Configurable: Customize debounce time, revalidation URL, and cache tags.
- Flexible Collection Support: Choose specific collections to enable cache clearing on.
- Clone or copy this plugin into your project.
- Install the necessary dependencies.
- Import and use the plugin in your Payload CMS setup.
In your payload.config.ts
(or JavaScript equivalent), use the plugin like this:
import { debouncedCacheClearer } from 'path/to/debounced-cache-clearer';
export default {
plugins: [
debouncedCacheClearer({
activeCollections: ['pages', 'offices'], // Specify collections to target
revalidateUrl: `${process.env.NEXT_PUBLIC_FRONTEND_URL}/api/revalidate`, // The URL for revalidation API
tags: ['global'], // Cache tags
revalidateKey: process.env.REVALIDATE_KEY ?? '', // Revalidate key for security
debounceTime: 600, // Debounce time in seconds (default: 600 seconds)
}),
],
}
In order to make the system work automatically, we need to set up a cron job to trigger the API endpoint at regular intervals. The cron job will make a POST request to the following URL every 5 or 10 minutes:
-
URL:
/api/globals/ev_cache_settings/debounced-data
-
Method:
POST
-
Accepted Parameter:
-
revalidateKey
(required, string): The secret key used for authentication.
-
Note: Make sure the
revalidateKey
in the request matches the key configured in your Payload CMS setup to ensure secure access.