Table of Contents
- Installing the plugin
- Using the plugin
- How does it work?
REST Cache is a plugin for WordPress, which significantly speeds up calls to the REST API.
Installing the plugin
The plugin is currently pending review from the WordPress team and will be available through WordPress’ built in plugin menu shortly.
Using the plugin
Usage is as simple as installing and activating the plugin. Once you’ve taken those steps, congratulations! Your REST API now has rocket boosters.
Managing the cache
Go to Dashboard → REST Cache to manage the plugin.
The Records tab lets you view information about stored records. You can see the URI associated with any stored records, where the cache record is stored, the size of the record and how many hits the record has had.
If you’d like to clear specific cache records, rather than completely disabling REST Caching, you can do that from this screen.
You can also clear the entire cache from this panel using the buttons at the top left. If you need to observe how the cache works with stored records without reloading the page, there is a button provided to refresh the record table here also.
Defining Rules (Advanced)
This section is intended for advanced users who understand how REST routing works and wish to alter the caches behaviour.
This panel allows you to define rules to include or exclude specific routes based on a pattern. Regular expressions are supported here, as well as a priority. Rules are tested in order of priority, ascending. So a lower number in the priority field means those rules will be tested against first.
At present, the plugin only has one setting – the default lifespan for a caching record. This defaults to 4 days. You can alter this as you need.
How does it work?
In the context of WP Google Maps, the REST API can be used to load marker data. When this configuration is used and a visitor loads a page with a map on it, WP Google Maps will contact the sites REST API to request the marker data. The marker data is then retrieved from the database, a response is formed, and then that data is sent back to the visitors browser for display.
When there is a large amount of complex data, this can be time consuming.
When REST Cache is running, it will pick up on that response, and if the right conditions for a caching are met, the response will be stored for a pre-determined amount of time.
Subsequent REST requests for the same information will cause the cache to completely bypass WordPress and all associated overheads.
As you can see, without REST Cache, this particular example takes 6.16 seconds to load. With REST Cache, this example takes just 46 milliseconds to load. That’s over 100 times faster than without caching.
This also almost completely eliminates the load on your sites database. In fact, the REST cache will only trigger a single database operation – which is used to increment the “hits” counter for any given record. This is done after the cached response has been sent to the user and the connection has been cut, so the cache will serve up the response as fast as it possibly can, cut the connection so as not to keep the user waiting, then make a single, small database query to increment the hits for that record.
Contributions are welcome on the plugins GitHub repository. Feel free to make suggestions via issues, fork or submit pull requests.
You’ll need NPM and Composer to work with the plugin. Please see the repository for specific instructions.