Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching JSON data

I've never really cached data before, but I feel as though it will greatly improve my site.

Basically, I pull JSON data from an external API that helps the basic functionality of my website. This info doesn't really change that often yet my users pull the info from the API thousands of times a day. If it updated once a day, that would be fine. I want to run a cron job daily that will pull the info and update the cache.

I already tried a few different things, both pulled using PHP:

1) Store data in an SQL table
    I had it working, but there's no reason why I should ping the database each time when I can just store it in basic HTML/JSON.

2) .JSON file (using fwrite)
    I had it stored, but the only way this worked is if the .getJSON() callback function is prepended to the JSON data and then the data is surrounded by parentheses (making it jsonp, I believe).

Does anyone have any advice or any directions to lead me in? As I said, I've never really done anything like this so I don't even know if I'm remotely headed in the right direction.

Edit:

Okay so I talked to my hosting and since I'm on a shared hosting (dreamhost) I can't install memcached, which sucks. The only info they could give me was that if it is on http://pecl.php.net/ then I can most likely use it. They said APC is available. I'm not sure if this fits my problem. I'd like to be able to access the cache directly in jQuery. Thanks

like image 800
switz Avatar asked Jul 22 '11 18:07

switz


People also ask

Do JSON files get cached?

If the JSON file is being read and used by PHP on the server then it won't (can't!) be cached by the user's browser. The resulting pages might be cached, though, either by the browser or by some intervening proxy, or both.

Can JSON be stored in Redis?

With this launch, application developers can effortlessly store, fetch, and update their JSON data inside Redis without needing to manage custom code for serialization and deserialization.

Is JSON a good way to store data?

JSON is perfect for storing temporary data. For example, temporary data can be user-generated data, such as a submitted form on a website. JSON can also be used as a data format for any programming language to provide a high level of interoperability.

Do browsers cache JSON?

Yes. Caching related headers and (how they are handled) work for all HTTP resources.


2 Answers

You can use memcached. Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Very easy to implement and has a low system footprint.

like image 198
Chris Ledet Avatar answered Sep 28 '22 10:09

Chris Ledet


Since you can't use memcached, go back to your database option and store it in a table using the MEMORY engine.

like image 29
Justin ᚅᚔᚈᚄᚒᚔ Avatar answered Sep 28 '22 09:09

Justin ᚅᚔᚈᚄᚒᚔ