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

WordPress Cache API for WP-API json WordPress

  • REFUNDED

I'm currently using WP-API plugin to return json that I am currently using on my website:

http://fancysquares.com/

However, the json response time when I make an Ajax call takes close to a second or more. (click menu, projects, click on a project).

I've tried to use WP HTTP to store the data but I know I am doing it wrong.

Any tips or tricks would be greatly appreciated as to how to make this work (how to do it, is it stored in functions, do I need to create a plugin for it, etc):

example:
$response = wp_remote_retrieve_body(wp_remote_get("http://fancysquares.com/wp-json/posts?type=project"));

// // We are calling json_decode function that return object with data
$data = json_decode($response);

var_dump($data);

Answers (1)

2015-09-22

dimadin answers:

Are you using some form of WordPress Object Cache (Memcache(d), Redis... http://codex.wordpress.org/Class_Reference/WP_Object_Cache)?

Also, it looks like you are using older version of WP API. Do you want solution for it?


Rory Heaney comments:

Im using the current version of the API, the 2.0 is still in beta.

I am looking for a solution as I dont fully understand how to use what options are currently available


dimadin comments:

Well, WP API as a whole is beta, and proposed merge in core will be based on v2: https://make.wordpress.org/core/2015/09/21/wp-rest-api-merge-proposal/

Object cache generally should improve speed of your WordPress, not just for WP API, but it can't be used on all hostings.

Also, I think that problem is either somewhere on your hosting or you have some plugin/theme/custom code that makes this very slow, it should be that way for this types of requests.

I propose to you to see can you use WordPress Object Cache on your hosting before creating anything else.

Do you want transients only for this particular request? Because using transients for all type of requests is really quite time wasting and expensive, better thing would be to invest in better hosting.


Rory Heaney comments:

Wouldn't you wan to use transients to temporarily store jSon data for quicker calls?


Rory Heaney comments:

http://stackoverflow.com/questions/17612962/possible-to-cache-json-to-increase-performance-load-time


dimadin comments:

WordPress Transients are one form of caching in WordPress, the other is Object Cache.

Transients actually use Object Cache, but only if Object Cache is enabled, otherwise they use database.

Transients are used in two cases:
1) When Object Cache isn't enabled
2) When you want to ensure that data is cached, no matter if Object Cache is enabled, and no matter where cache is saved.

I believe that your problem is not because data is not cached, I think that there is some else problem with your server, but this can't be debugged without direct access to server.

To test this, here is a solution that you initially wanted. Snippet below will save data to cache, or in other words transients, for one day for requests to '/wp-json/posts?type=project'.

This will only work for this request, you can change it accordingly for other requests. It is tested so definitely uses transients. If your request is still slow, then it is not problem with caching but something else that is not really possible to investigate from here, I advise enabling Object Cache or contacting host, or, if you know what are you doing, further debugging for root of the problem.

Also note that cache is deleted when you save a new post of 'project' post type or when you make edit to existing post of 'project' post type.



/**
* Serve WP API request from cache if available.
*/
function md_wp_api_serve_cache( $result, $request ) {
// Check if this is request for posts
if ( '/posts' != $request->path ) {
return $result;
}

// Check if this is request for 'project' post type
if ( array( 'type' => 'project' ) != $request->params['GET'] ) {
return $result;
}

// Check if it is cached
if ( false === ( $cache = get_transient( 'md_wp_api_project_cache' ) ) ) {
// It is not cache so register cache saver
add_filter( 'json_serve_request', 'md_wp_api_save_to_cache', 10, 5 );

return $result;
}

// Use cache for result
$result = $cache;

return $result;
}
add_filter( 'json_pre_dispatch', 'md_wp_api_serve_cache', 11, 2 );

/**
* Save WP API request to cache.
*/
function md_wp_api_save_to_cache( $served, $result, $path, $method, $request ) {
// Save to cache for one day
set_transient( 'md_wp_api_project_cache', $result, DAY_IN_SECONDS );

return $served;
}

/**
* Delete WP API request from cache.
*/
function md_wp_api_delete_cache() {
delete_transient( 'md_wp_api_project_cache' );
}
add_action( 'save_post_project', 'md_wp_api_delete_cache' );