Created
June 8, 2011 21:14
-
-
Save clifton/1015420 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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