Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Caching JSON API plugin with W3 Total Cache WordPress

  • REFUNDED

We are getting slow responses to our JSON api after installing JSON API for WordPress. We use W3 total Cache and are looking for some help with caching calls after /api/.
Does anyone know the best way to do this?

Answers (1)

2014-09-30

John Cotton answers:

I don't use either of those plugins but a well-structured cache set up should look something like this:


function get_my_cache_value() {
$x = get_transient( 'x_value' );

if ( !$x ) {
$x = my_expensive_x_calc();

set_transient( 'x_value', $x, 60 * 60 * 24 ); // vary the time if you need to
}

return $x;
}


W3TC should play nicely with this, but there are reports otherwise:

https://wordpress.org/support/topic/self-diagnosed-and-fixed-w3-total-cache-bug-in-faulty-object-caching


jackstin comments:

Yeah im looking into this at the moment - [[LINK href="https://wordpress.org/support/topic/patch-adding-caching-benchmarking-and-setting-change-notices"]][[/LINK]]


jackstin comments:

https://wordpress.org/support/topic/patch-adding-caching-benchmarking-and-setting-change-notices


jackstin comments:

We found some code that works - https://gist.github.com/KATT/3349372

define.php

<?php
/**
* Check if content is JSON
*
* @param string $content
* @return boolean
*/
function w3_is_json($content) {
return json_decode($content) !== NULL;
}

/**
* Check if content is JSONP
*
* @param string $content
* @return boolean
*/
function w3_is_jsonp($content) {
// Extract json
$stripped = preg_replace("/^\s*\w+\s*\(/", "", $content);
$stripped = preg_replace("/\)\s*$/", "", $stripped);
return w3_is_json($stripped);
}