Skip to content

Instantly share code, notes, and snippets.

@ozio
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozio/4831e25af55b02c83e12 to your computer and use it in GitHub Desktop.
Save ozio/4831e25af55b02c83e12 to your computer and use it in GitHub Desktop.
lscache
'use strict';
var $ = require('jquery');
var lscache = require('lscache');
var Settings = require('./settings');
var createUrl = function(url, data) {
return url + '?' + $.param(data);
};
var call = function(method, url, data, cached) {
if (cached) {
var cachedKey = createUrl(url, data);
var cachedResponse = lscache.get(cachedKey);
if (cachedResponse) {
return $.Deferred().resolve(cachedKey);
}
}
var request = {
url: Settings.API_URL + url,
data,
type: method,
timeout: Settings.API_TIMEOUT
};
var xhr = $.ajax(request);
xhr.always(function(res, status, message) {
if (cached) {
lscache.set(createUrl(url, data), res, 10);
}
});
return xhr;
};
module.exports = {
getStuff: function(that) {
call('GET', '/stuff', { that }, true);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment