Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added back in my k-v cache.

  • Loading branch information...
commit 36efe46886494ca686d62f334927171a8be16b5c 1 parent 141b37d
@ironwallaby ironwallaby authored
Showing with 46 additions and 0 deletions.
  1. +46 −0 index.js
View
46 index.js
@@ -74,3 +74,49 @@ exports.timeBasedWithGrace = function(func, soft, hard) {
})
}
}
+
+exports.sizeBasedKeyValue = function(func, size) {
+ var cache = [],
+ callbacks = {}
+
+ size += size
+
+ return function(key, callback) {
+ /* Look up in the cache. */
+ var i
+
+ for(i = 0; i !== cache.length; i += 2)
+ if(cache[i] === key) {
+ if(i !== 0)
+ Array.prototype.unshift.apply(cache, cache.splice(i, 2))
+
+ return callback(null, cache[1])
+ }
+
+ /* Somebody else is already polling the backend. Get notified when they're
+ * done, and bail. */
+ if(callbacks[key]) {
+ callbacks[key].push(callback)
+ return
+ }
+
+ callbacks[key] = [callback]
+
+ /* Call the backing store. */
+ return func(key, function(err, value) {
+ /* Successful result? Then add it to the cache. */
+ if(!err) {
+ if(cache.length === size)
+ cache.length -= 2
+
+ cache.unshift(key, value)
+ }
+
+ /* Notify all the saved callbacks, and then clean up. */
+ while(callbacks[key].length)
+ callbacks[key].pop()(err, value)
+
+ delete callbacks[key]
+ })
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.