Home > Uncategorized > Using Parse.com as a Ajax caching proxy

Using Parse.com as a Ajax caching proxy

Plenty of API’s have usage restrictions, either calls per minute, or calls per day etc. But perhaps many of your users search for similar data, and that data remains static. If I’m looking data on the longitude and latitude of a town, then this data is not going to change, then “cloud” caching would be ideal. This solution is not designed for data that may change, i.e. the current weather,

I’ve decided to use Parse.com as the backend, because it has generous free usage limits, (1 milion queries per month), it’s got a Javascript API, and it’s well known, and you can export your data if you ever wanted to move to another system.

So, lets take a simple ajax get request, in the form $.get(url, callback) and write a function with the same signature 

function cachedAjaxGet(url,callback)
{
var query = new Parse.Query(AjaxCache);
query.equalTo(“url”, url);
query.first({
success: function(object) {
if (object == undefined)
{
console.log(“cache-miss”);
$.get(url,function(data)
{
var ajaxCache = new AjaxCache();
ajaxCache.save({data: JSON.stringify(data), url: url},
{
success: function(object)
{
console.log(“stored as ” + object.id);
}
});
callback(data);
});
}
else
{
console.log(“cache-hit”);
var result = JSON.parse(object.get(‘data’));
callback(result);
}
}});
}

Then, don’t forget to initialize parse.com as follows

Parse.initialize(“….”, “…”);
var AjaxCache = Parse.Object.extend(“AjaxCache”);

And of course, include the Parse.com library from the CDN:

<script src=”http://www.parsecdn.com/js/parse-1.2.8.min.js”></script&gt;;

 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: