Is it legimate an/or possible to cache API results?

  • 1
  • Question
  • Updated 3 years ago
what counts as an api call? My site, which uses the NWS now, would qualify for the free version, but just barely. On the other hand, it seems like I could cache the weather pages for 15 minutes or a half hour and guarantee that it will always be under the maximum. Is that legitimate?
Photo of zpinhead

zpinhead

  • 1 Post
  • 0 Reply Likes

Posted 7 years ago

  • 1
Photo of jbriano

jbriano

  • 2 Posts
  • 0 Reply Likes
How would you cache the pages?
Photo of Brendan Hayes

Brendan Hayes, Official Rep

  • 962 Posts
  • 123 Reply Likes
Yes, you can cache. I recommend 5-15 minute caches for current conditions and 4-6 hour caches for forecast.
Photo of Jeremy Knight

Jeremy Knight

  • 2 Posts
  • 0 Reply Likes
Is caching unlimited? I'm interested in historical monthly averages. Once I've downloaded the data for a specific location, can I store that data in my own database permanently? Because they are historical averages, I don't need them to be updated frequently. 
(Edited)
Photo of Ravi Yadav

Ravi Yadav

  • 82 Posts
  • 9 Reply Likes
You can cache the results for a particular location and save them. The caching limit here would be your API call limit. 
Photo of juanbond

juanbond

  • 20 Posts
  • 8 Reply Likes
Yes, by all means cache what you get back from the API... Not only does this save your quota and potentially avoid unnecessary calls to non-updated data, but it's just good web-programming practice and saves bandwidth for all parties involved.

@jbriano - there are a few good ways to cache data like this, one way is to database it.
Photo of Jaace

Jaace

  • 1 Post
  • 0 Reply Likes
Is there a good way to store either an XML or JSON result in a cookie (or a different way I'm not thinking of that doesn't require a database)?

I'm trying to making a simple example for learning purposes and I cannot get the SimpleXMLELement to store in a cookie because it won't (I'm using PHP)... so now I'm trying to use JSON instead but I'd appreciate any recommendations for doing this the "right" way.

Any suggestions? Thanks!
Photo of ryanatrefresh

ryanatrefresh

  • 1 Post
  • 0 Reply Likes
I used localStorage which is widely supported. Here is a code sample with a 2 hour timeout.

function weatherReport(parsed_json){

// your function
}

function fetchWeather(){
$.ajax({
url : "http://api.wunderground.com/api/APIKEYHERE/geolookup/conditions/q/VA/Charlottesville.json",
dataType : "jsonp",
success : function(data) {
var appTime = new Date().getTime();
// Append it to the end of the full data object
data.myTime = appTime;
// Stringify so we can store it in localStorage
var jsonStore = JSON.stringify(data);
localStorage.setItem('bulk_json',jsonStore);
weatherReport(data);
}
});
} // .fetchWeather

if ( localStorage.getItem('bulk_json') === null ) {
// No storage set, go get new data
fetchWeather();
} else {
var minutes=1000*60;
var hours=minutes*60;
var now = new Date().getTime().toString();
// Grab the dataset from storage
var parsed = JSON.parse(localStorage.getItem('bulk_json'));
// Older than two (2) hours? get rid of it.
if( now > (parsed['myTime'] + (2*hours) ) ){
fetchWeather();
} else {
weatherReport(parsed);
}
}