Skip to content

Instantly share code, notes, and snippets.

@clifton
Created June 8, 2011 21:14
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 clifton/1015420 to your computer and use it in GitHub Desktop.
Save clifton/1015420 to your computer and use it in GitHub Desktop.
// requires Store.js
OrgSync.cache = (function(store) {
if (!store || typeof store.get !== 'function' || typeof store.set !== 'function') {
throw new Error("Store API not defined.");
}
var api = {};
var now = function () {
return parseInt((new Date).getTime() / 1000);
};
var is_expired = function (data) {
if (!data || !data.expires || data.expires >= now()) {
console.log("data expired!");
} else {
console.log("data NOT expired!");
}
return !data || !data.expires || data.expires >= now();
}
api.cas = function () {
if (arguments.length < 2) {
throw new Error("Usage: cas(key, [expires_in,], callback, [callback_arg])");
}
var args = Array.prototype.slice.call(arguments),
key = args.shift(),
expires_in = typeof args[0] === 'number' ? args.shift() : null,
on_miss = args.shift(),
on_miss_args = args;
return on_miss.apply(null, on_miss_args);
};
return api;
})(store);
// OrgSync.cache.cas("test key", function() { return arguments.length; }, 1,2,3) === 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment