Clearing the Cache

The Guardian library provides a method to clear all cache entries associated with a specific Guardian instance. This can be useful when you want to reset all rate limiting and error handling counters for a particular identifier.

Using the clearCache() method

To clear the cache, you need an instantiated Guardian instance. Here's how you can use the clearCache() method:

// Assuming you have an instantiated Guardian instance
$guardian = new Guardian('your-identifier', $cacheImplementation);

// Clear the cache for this Guardian instance
$result = $guardian->clearCache();

if ($result) {
    echo "Cache cleared successfully!";
} else {
    echo "Some or all cache entries could not be cleared.";
}

Important Notes:

  1. Instantiated Guardian Required: You must have an instantiated Guardian object to call the clearCache() method. This method is not static and operates on the specific instance's cache entries.

  2. Identifier-Specific: The clearCache() method only clears cache entries related to the Guardian instance's identifier. It does not affect cache entries for other Guardian instances or other parts of your application.

  3. Return Value: The method returns a boolean value:

    • true if all cache entries were successfully cleared

    • false if some or all cache entries could not be cleared

  4. Cache Implementation: The effectiveness of this method depends on the underlying cache implementation you're using (e.g., Redis, file system, database). Ensure your cache system supports deletion operations.

  5. Use with Caution: Clearing the cache resets all rate limiting and error handling counters. This could potentially allow previously rate-limited operations to proceed. Use this method judiciously, typically for maintenance or reset purposes.

When to Use clearCache()

Consider using clearCache() in scenarios such as:

  • Resetting rate limits after a maintenance period

  • Clearing error counters after resolving a system-wide issue

  • Implementing an admin function to reset limits for a specific user or service

Remember, you need access to the specific Guardian instance to clear its cache. In a typical application setup, you might need to retrieve or recreate the Guardian instance with the correct identifier before calling clearCache().

Last updated