Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching JSON with Cloudflare

Tags:

cloudflare

I am developing a backend system for my application on Google App Engine.

My application and backend server communicating with json. Like http://server.example.com/api/check_status/3838373.json or only http://server.example.com/api/check_status/3838373/

And I am planning to use CloudFlare for caching JSON pages.

Which one I should use on header? :

Content-type: application/json
Content-type: text/html

Is CloudFlare cache my server's responses to reduce my costs? Because I'll not use CSS, image, etc.

like image 669
ecabuk Avatar asked Jul 19 '12 11:07

ecabuk


2 Answers

The standard Cloudflare cache level (under your domain's Performance Settings) is set to Standard/Aggressive, meaning it caches only certain types by default scripts, stylesheets, images. Aggressive caching won't cache normal web pages (ie at a directory location or *.html) and won't cache JSON. All of this is based on the URL pattern (e.g. does it end in .jpg?) and regardless of the Content-Type header.

The global setting can only be made less aggressive, not more, so you'll need to setup one or more Page Rules to match those URLs, using Cache Everything as the custom cache rule.

http://blog.cloudflare.com/introducing-pagerules-advanced-caching

BTW I wouldn't recommend using an HTML Content-Type for a JSON response.

like image 166
echtish Avatar answered Nov 18 '22 17:11

echtish


By default, Cloudflare does not cache JSON file. I've ended up with config a new page rule:

https:/domain.com/sub-directiory/*.json*
  • Cache level: Cache Everything
  • Browser Cache TTL: set a timeout
  • Edge Cache TTL: set a timeout

Cloudflare page rule for json

Hope it saves someone's day.

like image 31
mihnsen Avatar answered Nov 18 '22 18:11

mihnsen