@extravirgin/debounced-cache-clearer
v1.0.16
Published
Payload 3.0 plugin that automatically deletes cache 10 minutes after changes in any target collection and provides an option for manual cache deletion.
Readme
Debounced Cache Clearer Plugin for Payload CMS 3.0
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.
Features
- 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.
Installation
- Clone or copy this plugin into your project.
- Install the necessary dependencies.
- Import and use the plugin in your Payload CMS setup.
Usage
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)
}),
],
}Setting Up the Cron Job
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:
API Endpoint
- 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
revalidateKeyin the request matches the key configured in your Payload CMS setup to ensure secure access.
