Forum Discussion

snaffle's avatar
snaffle
Expert
10 years ago

Advice on performance for external API query

Hi all, I'm looking for a bit of advice on improving the performance of a very simple API call that I've used to develop a Joomla CMS Module in PHP for use on a client's main website. The purpo...
  • luk's avatar
    luk
    10 years ago

    snaffle a few elaborations on the points made above:

     

    a) Cache the output of your Lithium API call, to do that you have to create an endpoint where you could also merge the data from the two calls into one dataset and then cache it for later reuse, which you do with something like that 

     

    <#assign data = appcache.set("externalfeed", yourdatainavariable) />

    Depending on how frequent the data should refresh, you have to contact support so they can adjust (lower) the cache expiry time for the appcache. In your endpoint you can then check if there is something stored for that cache-key and if so return that, else make the API calls and regenerate the data, e.g.

     

    <#if appcache.get("externalfeed", "")?has_content>
        <#-- output your data, be aware that you cannot just print freemarker node objects, so you have to convert them somehow to json/xml, which is not easy btw... -->
    <#else>
        <#-- re-fetch the data and put it into the cache -->
        ...
        <#assign data = appcache.set("externalfeed", yourdatainavariable) />
    </#if>

    But this is basically putting the work from your PHP script to Lithium which requires some FreeMarker/Lithium API knowledge.

     

    If we say that is not available then options to maybe slightly improve the performance is

     

    a) to simply use AJAX/JavaScript to fetch the content from the PHP script after the page has initially loaded and inject it to the right place, so the main content will be there while the AJAX call is getting the "slow" content from Lithium. This would be the easiest solution but doesn't actually change the performance, it just changes the perception =).

    b) Go away from parsing the response XML in PHP and just use the restapi.response_format=json URL parameter for your API calls that will return JSON instead of XML, which you can parse much more efficiently in PHP with json_decode(data, true); but I guess this will not significantly improve the performance of the whole thing...so just a micro-optimization and more elegant solution.

     

    if you have FreeMarker knowledge you can try to use API v2 message resource together with the restd context object inside an endpoint, the restd context object can actually return the data as a JSON string inside FreeMarker code (but you will not be able to modify the fetched data when doing that!) and API v2 would maybe allow to combine your two calls into one with an appropriate query, which means you can just cache and output the response without further worries about how to output it...