Manually flush cache
-
Hey,
I am using only custom endpoints.
Is there any way to flush the cache manually?
Best,
Adam
-
Hi @adamhammad
Yes, there are multiple ways of manually clearing the cache:
- Clear all caches, by clicking the ‘Clear REST Cache’ button in the wp-admin bar (top of your screen)
- Clearing a selection of caches, by going to Settings > WP REST Cache > Endpoint API Caches, select one or more caches and then Bulk Actions > Flush Cache > Apply
- Clearing one single cache, by going to Settings > WP REST Cache > Endpoint API Caches, hover over the cache you want to flush and click ‘Flush Cache’
I hop this answers your question.
Hey @acato
Is there any way to do this programatically?
Hi @adamhammad
At this point there are only two functions available for programatically flushing caches:
1. Flush all caches of a specific object type
Example:
\WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_object_type_caches( 'products' );
This deletes all caches for object typeproducts
2. Flush a specific cache
Example:
\WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache( '8dbc9f7badb0ed2d50f1eaf200422a49' );
Unfortunately as you can see, you would need the cache key for that. Which by the way is simply a md5 hash of the endpoint path with query parameters. So forhttps://www.domain.com/wp-json/wp/v2/pages
the cache key would bemd5( '/wp-json/wp/v2/pages' );
. However keep in mind that any query parameters should be ksort’ed.Your question makes me however realize that we should add more (better usable) functions for flushing caches programatically. I will discuss this internally and add some functions in the near future.
Do you have any suggestions on how you would like to do this? I.e. flushing all caches for a specific endpoint regardless of the query parameters, flushing a cache by it’s specific path (without the need to hash it yourself), … ?Okay cool I can use those functions that’s great. Converting the endpoint path to md5 isn’t a problem seems to be matching here from a quick test.
Those use cases you describe is basically what i’m looking to achieve.
– Flushing all caches for an endpoint regardless of query params
– Flushing cache by specific path/endpointWould be a good place to start.
Hi @adamhammad
I will add it to my todo-list. Unfortunately I don’t have any time left to work on it this week.
No problem, the information you’ve provided is plenty for my solution.
I have some endpoints that check authentication inside them, I would like a way to make sure any authenticated requests by admins are not cached. Any ideas on how to approach this?
Hi @adamhammad
We just released a new version of our plugin which now includes a function to programatically flush caches by endpoint. So for instance if you want to flush the caches for the endpoint
/wp-json/wp/v2/pages
you can do so like this:
\WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache_by_endpoint( '/wp-json/wp/v2/pages' );
This function accepts two extra parameters:
1.$strictness
: This defines how strict you want the caches to match, you have three options for this:
\WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_STRICT
: Delete caches by an endpoint path matching only this exact same endpoint path (and query params).
\WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_PARAMS
: Delete caches by an endpoint path matching the exact same endpoint path with any query params that might have been used.
\WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_LOOSE
: Delete caches by an endpoint path matching any cache that was called starting with the endpoint path (ignoring any query params or subpaths following the given path).2.
$force
: Should the caches be deleted ($force = true) or just flushed ($force = false).About your endpoint that checks authentication inside them: are you sure you actually want this endpoint to be cached?
At this point I don’t really see an option to do this, the code where we check if we should cache or not (
\includes\api\class-endpoint-api.php
line 203) is executed from a must use plugin and therefore is executed before any plugin or theme code. So adding a filter there will not help you very much since theapply_filters
is than executed before you added your filter.
I am going to think about a solution for this and discuss it internally, maybe we can find a solution that works for you.Thanks for the update! It works great, definitely fixed the issue I was having before with detecting object type and throwing an error.
I think I want to cache the endpoint, this is my use case:
I have a product catalogue that is cached. Admin users can see ‘draft’ and ‘private’ status products, whereas Customers can only see ‘publish’ products. So the cached responses won’t actually return what an admin should see, and if an admin sets the cached response then it will show ‘draft’ and ‘private’ products to customers.
I guess I could create an endpoint specifically for admin users to access the catalogue.
Let me know your thoughts.
Hi @adamhammad
Well creating a new (duplicate) endpoint doesn’t feel right.
After some consideration I think there are two possible solutions to this problem:
1. We add a check to see if the current user is authenticated and if so, do not use caching. But that would mean that all responses for authenticated users are no longer using caching. That doesn’t feel right either.
2. We can implement the option for you to use an extra parameter to tell the WP REST Cache plugin to skip caching. That way you can decide for each request if you want to use caching or not. So for admin users you would add an extra parameter to the REST call. Does that sound like an option to you?
Sorry thought I replied to this.
Number 2 sounds like a more flexible option, I can see this being used for more than just authentication purposes. e.g. Do not cache based on an environment variable.
Hi @adamhammad
We have just released a new verion of the plugin. We have added a GET-parameter you can use to skip caching. Simply add
?skip_cache=1
to you REST call.Please let us know if this works for you!
EDIT: Nevermind, will get back to you when I’ve investigated a little bit more ?? Thank you for an awesome plugin!
- The topic ‘Manually flush cache’ is closed to new replies.