• I’m hoping someone here can answer this for me. Is there a way to cache the api’s json responses? I would assume you could use wordpress transients to do this but I’m not sure how to even achieve this. Basically what I’m after is this

    1. http call to an endpoint with query params
    2. check for transient containing json result for that api call
    3. If transient exists, output json (skip all json_prepare_post filters)
    4. If transient doesn’t exist, query the database and filter the results like normal.

    Has anyone achieve this? My api calls take around 0.5 seconds to complete which by my standards is pretty slow. I’m hoping I can use a caching method to speed this up.

    https://www.ads-software.com/plugins/json-rest-api/

Viewing 15 replies - 1 through 15 (of 17 total)
  • On the hunt for this, too.

    At the moment, if you use composer in your stack, you can use this.

    https://github.com/Shelob9/jp-rest-cache

    I don’t use composer, so I’m still on the hunt. Might have to write something myself :).

    This article has some good info.

    any success guys?

    I haven’t come across anything, but I have noticed when using the API that sometimes the posts don’t update right away. I think they may have built caching into it now. Best to look at the documentation for the plugin to confirm if that’s the case before exploring any further, @wordpressmenow.

    thanks Eric

    Thread Starter pleek

    (@pleek)

    I think I ended up adding some custom code to the rest api plugin to achieve this. Though I wouldn’t recommend it. Possible you could create a custom endpoint and wrap your endpoint in transient checks, though I’m not sure off the top of my head how to do that.

    hi eric

    your idea sound good and in the right direction!

    possible for you to share the code you did? maybe i would get an idea from that on how to finalize it ??

    thanks!

    Thread Starter pleek

    (@pleek)

    I never coded a complete custom endpoint with the transient but that’s how I would do it if I had to do it again. I would start by reading the rest api doc on adding custom endpoints as well as the wordpress transient api doc

    https://v2.wp-api.org/extending/adding/
    https://codex.www.ads-software.com/Transients_API

    Ideally your custom endpoint would be /wp-json/wp/v2/cached/ and then whatever the actual url endpoint you want. Take /wp-json/wp/v2/posts for instance. Your custom end point would be /wp-json/wp/v2/cached/posts (cached being the endpoint and posts being what you want to get) and in your custom end point you would check for a transient, and if one doesn’t exist you would do a second rest api call to /wp-json/wp/v2/posts and cache that result in a transient.

    You’d basically be putting a layer on top of the api to do your caching.

    Disclaimer: I haven’t worked with wp-rest api in probably 6 months or more. This post is based solely on my knowledge of the api as of then, its quite possible the plugin has advanced and that there is a built in way to do this or a better/recommended way to do this.

    thanks a lot eric!

    i will try your idea and will update if i got it to work ??

    sorry :/ your name is pleek… i thought i was still talking to eric…

    my bad.

    THANK YOU *** PLEEK ***

    Thread Starter pleek

    (@pleek)

    No problem lol. If you get something working post it here. Good luck!

    sure will! thanks ??

    By the way, that wasn’t Eric replying to you. But do share if you get something working! ??

    I am also very interested in this. Has anyone had success yet?

    hi nate

    yes – but i’m using the wc REST API legacy (not the new WP REST).

    relevant?

    No I am using new WP REST, thanks.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Is there a way to cache json responses’ is closed to new replies.