Cache delete action
About cache
The OTYS plugin uses WordPress transients to store responses of the OTYS API requests. These transients are stored in the WordPress database. By caching the responses it's not necessary for the plugin to do API requests every time which makes the plugin allot faster.
There are three scenario's of how cache can be removed:
By the cache expiring. All cache has an expiration date specified, when the cache expires it will not be used anymore and will be deleted by a WordPress cron event (which gets registered by the plugin) within 24hours.
By a manual action. For example clicking the 'remove all cache' button in the plugin admin panel.
By a webhook. OTYS uses webhooks to refresh relevant cache based on events happening in OTYS Go!. (also see Webhook action)
The OTYS plugin does not use the 'delete_transient' function of WordPress when removing many cache records. This is due the many records that will get removed and could be triggering the the delete_transient function 1000+ times, since there could be allot of cache records created by the plugin depending on how large your website is. Therefore OTYS has written efficient database queries to remove cache. The downside of not using the delete_transient function of WordPress is that you can't use the delete_transient_{transient} action. Therefore we have created our own actions when removing cache.
Use case
It might be that you as a developer need to some action when cache gets refreshed. A good example might be that you are using your own caching methods on top of the WordPress transients (which the plugin uses). This means that it's not enough to only refresh the transient cache, but also your other caching method needs to get a heads up that it needs to refresh it's cache.
How to use the webhook?
On the left you see an example of the use case above. We register a webhook by calling add_action('otys_cache', 'example_callback', 10, 0)
. After the cache gets deletes the action is called.
The following parameters available in the callback function;
string $type | Describes what type of action got triggered i.e. 'refresh', 'delete_all', 'delete_expired', 'delete' |
array $transients | An array of all deleted transient names |