1

Is it possible to create static JSON file using the Element API plugin?

What I then want to do was cache that file probably using Varnish or push it to an S3 bucket.

If I was to cache it I would need to look at purging the URL when an entry is added or edited. If I pushed it to S3, then I would either need to overwrite the file or manage it another way.

What would be the best way to handle this, and could you easily create a static JSON file with caching and purging abilities?

  • Would it be an option to cache it locally? The Craft HTMLCache plugin (github.com/craftapi/htmlcache) is currently in beta, but busts the cache when an entry is edited and automatically expires after an hour.. – chris Feb 26 '16 at 14:20
  • Was HTMLCache useful in this case or did you go with another method? I'm in a similar situation now so just curious as to what worked for other people. – Ian Aug 23 '16 at 8:39
3

Not sure how you can do this, but you could manually print the response to a file yourself on a regular basis (as a cron job) using wget:

This:

wget -S http://craft.fira/fullmembers.json

…saves the JSON into a file named fullmembers.json into the directory it was run from.

Here is the solution that I have used in a cron job that runs every minute:

wget -O - http://www.fira.co.uk/fullmembers.json > /home/forge/www.fira.co.uk/public/jsoncache/fullmembers-temp.json
rm /home/forge/www.fira.co.uk/public/jsoncache/fullmembers.json
mv /home/forge/www.fira.co.uk/public/jsoncache/fullmembers-temp.json /home/forge/www.fira.co.uk/public/jsoncache/fullmembers.json
  • Interesting. Have you noticed a big improvement in the speed of data access from the app(s) consuming the API? At the moment this is a bottleneck for me. I'm not sure if it's the server response time or the DB queries – Ian Aug 22 '16 at 14:36
  • For what I have been using it for the response time has been very quick. I have made sure to keep the limit fairly low and rinse pagination, otherwise that does slow it down, plus I have been using eager loading where I can. – zizther Aug 23 '16 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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