admin管理员组文章数量:1278983
I have a simple endpoint. Its GET, I pass it an ID parameter and it uses this ID to make a curl call. The endpoint then responds with a couple pieces of info json_encoded.
The issue is that this endpoint keeps caching its results. How do I keep this from happening?
Couple notes:
- No caching plugins installed
- WP config doesn't note caching
The endpoint code is pretty simple:
// Get Number of people in line
add_action( 'rest_api_init', function () {
register_rest_route( 'cc/v1', '/in_line/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'in_line',
'args' => [
'id'
],
) );
} );
function in_line($data) {
//Do a bunch of Curl stuff
$response['queue'] = $number;
$response['queueID'] = $data['id'];
return json_encode($response);
}
I call the endpoint via jQuery ajax.
I have a simple endpoint. Its GET, I pass it an ID parameter and it uses this ID to make a curl call. The endpoint then responds with a couple pieces of info json_encoded.
The issue is that this endpoint keeps caching its results. How do I keep this from happening?
Couple notes:
- No caching plugins installed
- WP config doesn't note caching
The endpoint code is pretty simple:
// Get Number of people in line
add_action( 'rest_api_init', function () {
register_rest_route( 'cc/v1', '/in_line/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'in_line',
'args' => [
'id'
],
) );
} );
function in_line($data) {
//Do a bunch of Curl stuff
$response['queue'] = $number;
$response['queueID'] = $data['id'];
return json_encode($response);
}
I call the endpoint via jQuery ajax.
Share Improve this question asked Mar 1, 2018 at 4:08 TJ SherrillTJ Sherrill 5856 gold badges14 silver badges30 bronze badges 2- 1 As far as I'm aware the REST API doesn't cache its results. Your code mentions "Do a bunch of Curl stuff". Are you sure that's not cached? – Jacob Peattie Commented Mar 1, 2018 at 4:18
- 1 How do you know its cached, and where is it cached? – Mark Kaplun Commented Mar 1, 2018 at 4:25
3 Answers
Reset to default 4If you have access to your request header you can add the line.
Cache-Control: private
or Cache-Control: no-cache
. This will force well-behaved hosts to send you fresh results.
You should create a new instance from WP_REST_Response
to set the Cache-Control value.
<?php
// Get Number of people in line
add_action( 'rest_api_init', function () {
register_rest_route( 'cc/v1', '/in_line/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'in_line',
'args' => [
'id'
],
) );
} );
function in_line($data) {
//Do a bunch of Curl stuff
$response['queue'] = $number;
$response['queueID'] = $data['id'];
$result = new WP_REST_Response($response, 200);
// Set headers.
$result->set_headers(array('Cache-Control' => 'no-cache'));
return $result;
}
Based on @fränk 's comment the correct and more elegant way will be like this:
<?php
// Get Number of people in line
add_action( 'rest_api_init', function () {
register_rest_route( 'cc/v1', '/in_line/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'in_line',
'args' => [
'id'
],
) );
} );
function in_line($data) {
//Do a bunch of Curl stuff
$response['queue'] = $number;
$response['queueID'] = $data['id'];
nocache_headers();
$result = new WP_REST_Response($response, 200);
return $result;
}
本文标签: cacheHow to stop WP API endpoint from caching
版权声明:本文标题:cache - How to stop WP API endpoint from caching? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741231260a2362148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论