3

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:

  1. No caching plugins installed
  2. 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.

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? Commented Mar 1, 2018 at 4:18
  • 1
    How do you know its cached, and where is it cached? Commented Mar 1, 2018 at 4:25

3 Answers 3

4

If 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.

2

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;
}
1
  • 1
    Rather than setting the cache header manually, it is preferable to use wp_get_nocache_headers(). This will set the nocache headers used by WordPress Core. This ensures compatibility with a wider array of hosting environments. Batcache for example (which is used by a lot of hosts) will look for a max-age value, and only if it is set will bypass cache.
    – Fränk
    Commented Jul 21, 2020 at 13:38
1

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.