airesvsg / wp-rest-api-cache Goto Github PK
View Code? Open in Web Editor NEWEnable caching for WordPress REST API and increase speed of your application
Home Page: http://wordpress.org/plugins/wp-rest-api-cache
Enable caching for WordPress REST API and increase speed of your application
Home Page: http://wordpress.org/plugins/wp-rest-api-cache
Hi,
Using register_rest_route, I have set the following permission_callback on my custom route : 'permission_callback' => [$this, 'checkAPIAccess'
.
The first time I call my route, the permission_callback is executed. Once the route is cached, the response is sent without checking my permission_callback function.
Thanks.
The first response is ok:
https://share.getcloudapp.com/Kouqm0j0
And the response is getting cached:
https://share.getcloudapp.com/8Luj4B1L
But for the subsequent requests I get incorrect responses, the cached response is incorrect:
https://share.getcloudapp.com/rRu9Jyzw
The same happens for every REST API endpoint regardless if it is Wordpress or WooCommerce route.
I am using WooCommerce 4.0.1
[Enhancement]
So that a developer could trigger subsequent functions once the cache has been cleared. It would be great to have a way to hook into a successful clear. Perhaps something like rest_cache_cleared
?
I have modified my theme's functions.php to allow an unlimited maximum number of posts. My API call is: 'wp-json/wp/v2/posts?page=1&per_page=1000'
My functions.php modification is:
add_filter( 'rest_endpoints', function( $endpoints ){
if ( ! isset( $endpoints['/wp/v2/posts'] ) ) {
return $endpoints;
}
unset( $endpoints['/wp/v2/posts'][0]['args']['per_page']['maximum'] );
return $endpoints;
});
The caching function is working great, substantially reducing load times as expected! The issue I'm having is that for unclear reasons and after anywhere from 4-24 hours, the served cached file no stops including all posts and reverts to serving only the most recent 10 posts (as if the per_page parameter were ignored).
I am currently solving the issue by emptying the cache, after which it resumed working as expected. I've tried the following steps without success:
My current solution is to regularly monitor the cached file and manually empty the cache when I note that it is no longer serving the full number of posts. Any ideas on what might be going on?
Hi, I need to cache the rest api v3 of woocommerce. The products are updated weekly and I need to cache the woocommerce rest api v3 because it is used from the android and ios app to read the products to show and they are loading very slowly: it need about 1 second for each product to load. I think that caching the results should show these result faster.
When activated on a site with the Gutenberg plugin installed, the gutenberg editor just goes to a white screen. There are no console errors, no php errors, just a blank white screen when you put a post in edit mode.
Does the cache clear when a post (or page, custom post, etc) is updated or created? Or is there a way I can do that programmatically, eg, a function I can call on the post_updated
hook?
Hi airesvsg.
Is it possible to clear cache only for specific endpoints? I would like to clear cache immediately only for specific post types when I create new post or update existing.
For an API request to get all posts (/wp-json/wp/v2/posts) it takes around 4000ms.
When I enable the plugin it has no time difference. I assumed with the cache enabled this would be instant - as it would cache?
Perhaps there is a step I'm missing with installation.
WP REST API:
Version 2.0-beta15
WP REST API Cache:
Version 1.2.0
I'm trying to query my custom post type over the WP REST API with the cache plugin enabled.
On first call, or without cache, it renders the following correct JSON output:
[{
"id": 4220,
"date": "2016-11-18T15:03:52",
"date_gmt": "2016-11-18T14:03:52",
"guid": {
"rendered": "https:\/\/www.studioimnetz.de\/?post_type=bswp_paedi_winners&p=4220"
},
"modified": "2017-08-13T17:57:21",
"modified_gmt": "2017-08-13T15:57:21",
"slug": "mrwissen2go",
"status": "publish",
"type": "bswp_paedi_winners",
"link": "http:\/\/localhost\/wordpress_40\/paedi-winners\/mrwissen2go\/",
"title": {
"rendered": "MrWissen2go"
},
"content": {
"rendered": "",
"protected": false
},
"author": 1,
"featured_media": 0,
"template": "",
"meta": [],
"platforms": [2],
"_links": {
"self": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220"
}],
"collection": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners"
}],
"about": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/types\/bswp_paedi_winners"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users\/1"
}],
"version-history": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220\/revisions"
}],
"wp:attachment": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/media?parent=4220"
}],
"wp:term": [{
"taxonomy": "platform",
"embeddable": true,
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms?post=4220"
}],
"curies": [{
"name": "wp",
"href": "https:\/\/api.w.org\/{rel}",
"templated": true
}]
},
"_embedded": {
"author": [{
"id": 1,
"name": "benjamin.jentsch",
"url": "",
"description": "",
"link": "http:\/\/localhost\/wordpress_40\/author\/benjamin-jentsch\/",
"slug": "benjamin-jentsch",
"avatar_urls": {
"24": "http:\/\/2.gravatar.com\/avatar\/83bdd363b7932e80b3e1a8d989dc3238?s=24&d=mm&r=g",
"48": "http:\/\/2.gravatar.com\/avatar\/83bdd363b7932e80b3e1a8d989dc3238?s=48&d=mm&r=g",
"96": "http:\/\/2.gravatar.com\/avatar\/83bdd363b7932e80b3e1a8d989dc3238?s=96&d=mm&r=g"
},
"_links": {
"self": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users\/1"
}],
"collection": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users"
}]
}
}],
"wp:term": [
[{
"id": 2,
"link": "http:\/\/localhost\/wordpress_40\/platform\/web\/",
"name": "Web",
"slug": "web",
"taxonomy": "platform",
"_links": {
"self": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms\/2"
}],
"collection": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms"
}],
"about": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/taxonomies\/platform"
}],
"wp:post_type": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners?platforms=2"
}],
"curies": [{
"name": "wp",
"href": "https:\/\/api.w.org\/{rel}",
"templated": true
}]
}
}]
]
}
}]
However, when the cache delivers the cached version from the database, the result is as follows:
[{
"id": 4220,
"date": "2016-11-18T15:03:52",
"date_gmt": "2016-11-18T14:03:52",
"guid": {
"rendered": "https:\/\/www.studioimnetz.de\/?post_type=bswp_paedi_winners&p=4220"
},
"modified": "2017-08-13T17:57:21",
"modified_gmt": "2017-08-13T15:57:21",
"slug": "mrwissen2go",
"status": "publish",
"type": "bswp_paedi_winners",
"link": "http:\/\/localhost\/wordpress_40\/paedi-winners\/mrwissen2go\/",
"title": {
"rendered": "MrWissen2go"
},
"content": {
"rendered": "",
"protected": false
},
"author": 1,
"featured_media": 0,
"template": "",
"meta": [],
"platforms": [2],
"_links": {
"self": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220"
}],
"collection": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners"
}],
"about": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/types\/bswp_paedi_winners"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users\/1"
}],
"version-history": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220\/revisions"
}],
"wp:attachment": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/media?parent=4220"
}],
"wp:term": [{
"taxonomy": "platform",
"embeddable": true,
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms?post=4220"
}],
"curies": [{
"name": "wp",
"href": "https:\/\/api.w.org\/{rel}",
"templated": true
}]
},
"_embedded": {
"author": [
[{
"id": 4220,
"date": "2016-11-18T15:03:52",
"date_gmt": "2016-11-18T14:03:52",
"guid": {
"rendered": "https:\/\/www.studioimnetz.de\/?post_type=bswp_paedi_winners&p=4220"
},
"modified": "2017-08-13T17:57:21",
"modified_gmt": "2017-08-13T15:57:21",
"slug": "mrwissen2go",
"status": "publish",
"type": "bswp_paedi_winners",
"link": "http:\/\/localhost\/wordpress_40\/paedi-winners\/mrwissen2go\/",
"title": {
"rendered": "MrWissen2go"
},
"content": {
"rendered": "",
"protected": false
},
"author": 1,
"featured_media": 0,
"template": "",
"meta": [],
"platforms": [2],
"_links": {
"self": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220"
}],
"collection": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners"
}],
"about": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/types\/bswp_paedi_winners"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users\/1"
}],
"version-history": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220\/revisions"
}],
"wp:attachment": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/media?parent=4220"
}],
"wp:term": [{
"taxonomy": "platform",
"embeddable": true,
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms?post=4220"
}],
"curies": [{
"name": "wp",
"href": "https:\/\/api.w.org\/{rel}",
"templated": true
}]
}
}]
],
"wp:term": [
[{
"id": 4220,
"date": "2016-11-18T15:03:52",
"date_gmt": "2016-11-18T14:03:52",
"guid": {
"rendered": "https:\/\/www.studioimnetz.de\/?post_type=bswp_paedi_winners&p=4220"
},
"modified": "2017-08-13T17:57:21",
"modified_gmt": "2017-08-13T15:57:21",
"slug": "mrwissen2go",
"status": "publish",
"type": "bswp_paedi_winners",
"link": "http:\/\/localhost\/wordpress_40\/paedi-winners\/mrwissen2go\/",
"title": {
"rendered": "MrWissen2go"
},
"content": {
"rendered": "",
"protected": false
},
"author": 1,
"featured_media": 0,
"template": "",
"meta": [],
"platforms": [2],
"_links": {
"self": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220"
}],
"collection": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners"
}],
"about": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/types\/bswp_paedi_winners"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users\/1"
}],
"version-history": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220\/revisions"
}],
"wp:attachment": [{
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/media?parent=4220"
}],
"wp:term": [{
"taxonomy": "platform",
"embeddable": true,
"href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms?post=4220"
}],
"curies": [{
"name": "wp",
"href": "https:\/\/api.w.org\/{rel}",
"templated": true
}]
}
}]
]
}
}]
So the diff would be:
--- paedi_winners.json Tue Aug 15 16:11:58 2017
+++ paedi_winners_cached.json Tue Aug 15 16:11:58 2017
@@ -55,46 +55,115 @@
}]
},
"_embedded": {
- "author": [{
- "id": 1,
- "name": "benjamin.jentsch",
- "url": "",
- "description": "",
- "link": "http:\/\/localhost\/wordpress_40\/author\/benjamin-jentsch\/",
- "slug": "benjamin-jentsch",
- "avatar_urls": {
- "24": "http:\/\/2.gravatar.com\/avatar\/83bdd363b7932e80b3e1a8d989dc3238?s=24&d=mm&r=g",
- "48": "http:\/\/2.gravatar.com\/avatar\/83bdd363b7932e80b3e1a8d989dc3238?s=48&d=mm&r=g",
- "96": "http:\/\/2.gravatar.com\/avatar\/83bdd363b7932e80b3e1a8d989dc3238?s=96&d=mm&r=g"
- },
- "_links": {
- "self": [{
- "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users\/1"
- }],
- "collection": [{
- "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users"
- }]
- }
- }],
+ "author": [
+ [{
+ "id": 4220,
+ "date": "2016-11-18T15:03:52",
+ "date_gmt": "2016-11-18T14:03:52",
+ "guid": {
+ "rendered": "https:\/\/www.studioimnetz.de\/?post_type=bswp_paedi_winners&p=4220"
+ },
+ "modified": "2017-08-13T17:57:21",
+ "modified_gmt": "2017-08-13T15:57:21",
+ "slug": "mrwissen2go",
+ "status": "publish",
+ "type": "bswp_paedi_winners",
+ "link": "http:\/\/localhost\/wordpress_40\/paedi-winners\/mrwissen2go\/",
+ "title": {
+ "rendered": "MrWissen2go"
+ },
+ "content": {
+ "rendered": "",
+ "protected": false
+ },
+ "author": 1,
+ "featured_media": 0,
+ "template": "",
+ "meta": [],
+ "platforms": [2],
+ "_links": {
+ "self": [{
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220"
+ }],
+ "collection": [{
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners"
+ }],
+ "about": [{
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/types\/bswp_paedi_winners"
+ }],
+ "author": [{
+ "embeddable": true,
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users\/1"
+ }],
+ "version-history": [{
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220\/revisions"
+ }],
+ "wp:attachment": [{
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/media?parent=4220"
+ }],
+ "wp:term": [{
+ "taxonomy": "platform",
+ "embeddable": true,
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms?post=4220"
+ }],
+ "curies": [{
+ "name": "wp",
+ "href": "https:\/\/api.w.org\/{rel}",
+ "templated": true
+ }]
+ }
+ }]
+ ],
"wp:term": [
[{
- "id": 2,
- "link": "http:\/\/localhost\/wordpress_40\/platform\/web\/",
- "name": "Web",
- "slug": "web",
- "taxonomy": "platform",
+ "id": 4220,
+ "date": "2016-11-18T15:03:52",
+ "date_gmt": "2016-11-18T14:03:52",
+ "guid": {
+ "rendered": "https:\/\/www.studioimnetz.de\/?post_type=bswp_paedi_winners&p=4220"
+ },
+ "modified": "2017-08-13T17:57:21",
+ "modified_gmt": "2017-08-13T15:57:21",
+ "slug": "mrwissen2go",
+ "status": "publish",
+ "type": "bswp_paedi_winners",
+ "link": "http:\/\/localhost\/wordpress_40\/paedi-winners\/mrwissen2go\/",
+ "title": {
+ "rendered": "MrWissen2go"
+ },
+ "content": {
+ "rendered": "",
+ "protected": false
+ },
+ "author": 1,
+ "featured_media": 0,
+ "template": "",
+ "meta": [],
+ "platforms": [2],
"_links": {
"self": [{
- "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms\/2"
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220"
}],
"collection": [{
- "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms"
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners"
}],
"about": [{
- "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/taxonomies\/platform"
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/types\/bswp_paedi_winners"
}],
- "wp:post_type": [{
- "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners?platforms=2"
+ "author": [{
+ "embeddable": true,
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/users\/1"
+ }],
+ "version-history": [{
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/paedi_winners\/4220\/revisions"
+ }],
+ "wp:attachment": [{
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/media?parent=4220"
+ }],
+ "wp:term": [{
+ "taxonomy": "platform",
+ "embeddable": true,
+ "href": "http:\/\/localhost\/wordpress_40\/wp-json\/wp\/v2\/platforms?post=4220"
}],
"curies": [{
"name": "wp",
As you can see, the author
and wp:term
fields get replaced with the custom post type's content, which is not exactly what I want.
I'm not quite sure if this is a WP REST API Cache bug or a wordpress API core bug.
If you use a persistent object cache with WordPress (Memcached, Redis, etc), transients are stored there rather than in the database, so the clear_cache
function will not work with that setup
Hello,
My POST requests have cache, so how do you disable the cache for POST requests but not for GET requests on the URL wp-json/wp/v2/comments
thanks you
Your plugin is amazing !
Plugin not working on WP REST API V2 posts query with parameter _embed.
for example: http://example.com/wp-json/wp/v2/posts?_embed
I am embedding related media
in my requests for a custom post type. When I get a cached response, the media array (post._embedded['wp:featuredmedia']
) contains a copy of the array of posts I just received ( minus the _embedded
property on each of the elements ).
Hi @airesvsg,
I use "wp-rest-api-cache" plugin and I would like to disable the cache specifically for a custom route (using register_rest_route() method).
Is there a way to do that?
Thanks
Hi, I see this is quite a new plugin but it's just what I needed at the moment, so thanks!
However, the plugin doesn't seem to be working for me. My setup is that I have one domain and server hosting the wordpress site setup with the REST API and also with this plugin installed, and I have another domain and server hosting a frontend site that make GET request calls to the wordpress site for json data and I want that data to be cached.
I'm still developing the frontend locally at the moment and it's not cache the data. Am I missing some steps or something? Thanks again.
I'm not seeing any noticeable speed improvements using this plugin, is this just browser level caching or does it persist something to disk/mysql to speed up future requests for the same data?
It will be nice to include Last-Modified or ETag to rest_cache_headers filter
Regarding the contents of the nice filter-admin-show.gif:
add_action( 'a_hook', '__return_false' );
Please see https://developer.wordpress.org/reference/functions/__return_false/
I'm trying to build a progress bar for calls to my cached REST API. I'm able to set headers using "rest_cache_header", but I need a bit of help setting the proper value. It seems that strlen($response)
would work if $response were accessible. Any help would be greatly appreciated!
add_filter( 'rest_cache_headers', function( $headers ) {
$headers['Content-Length'] = VALUE;
return $headers;
} );
Hello,
I have installed WP REST API Cache and it is caching the rest api. However, the cache is not getting cleared after the new post publish
I have added the following code, but it is still not working
add_action( 'save_post', function( $post_id ) {
if ( class_exists( 'WP_REST_Cache' ) ) {
WP_REST_Cache::empty_cache();
}
} );
Also, when I try to delete cache manually, the newest post is not appearing on the rest API. The API is getting updated only when I disable this plugin
Could you please let me know how I can fix this issue?
I'm getting this kind of error just by installing & enabling the plugin:
[21-Mar-2017 08:46:03 UTC] PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in [].../wordpress/wp-includes/functions.php:435
Stack trace:
#0 [...]/wordpress/wp-includes/functions.php(435): serialize(Object(WP_REST_Response))
#1 [...]/wordpress/wp-includes/option.php(427): maybe_serialize(Object(WP_REST_Response))
#2 [...]/wordpress/wp-includes/option.php(730): add_option('_transient_rest...', Object(WP_REST_Response), '', 'no')
#3 [...]/dm-collection/plugins/wp-rest-api-cache/class-wp-rest-cache.php(63): set_transient('rest_cache_/wp-...', Object(WP_REST_Response), 3600)
#4 [...]/wordpress/wp-includes/class-wp-hook.php(298): WP_REST_Cache::pre_dispatch(Object(WP_REST_Response), Object(WP_REST_Server in [].../wordpress/wp-includes/functions.php on line 435
Any idea what's wrong?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.