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
clearCache()
methodTo clear the cache, you need an instantiated Guardian instance. Here's how you can use the clearCache()
method:
Important Notes:
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.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.Return Value: The method returns a boolean value:
true
if all cache entries were successfully clearedfalse
if some or all cache entries could not be cleared
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.
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()
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