Last active
December 21, 2015 13:39
-
-
Save mrlannigan/6314235 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
'use strict'; | |
/** | |
* Basic Caching Strategy Class file | |
* @author Julian Lannigan <[email protected]> | |
* @since 22AUG2013 | |
*/ | |
(function () { | |
/** | |
* Timeout Error Class | |
* @param {String} msg Timeout error message | |
* @extends {Error} | |
*/ | |
function TimeoutError(msg) { | |
Error.captureStackTrace && Error.captureStackTrace(this, this); | |
this.message = msg || 'Error'; | |
} | |
TimeoutError.super_ = Error; | |
TimeoutError.prototype = Object.create(Error.prototype, { | |
constructor: {value: TimeoutError, enumerable: false}, | |
name: {value: 'Timeout Error'} | |
}); | |
/** | |
* Basic Caching Strategy | |
*/ | |
function BasicCachingStrategy() {} | |
/** | |
* Default timeout length (ms) | |
* @type {Number} | |
*/ | |
BasicCachingStrategy.prototype.timeout = 5000; | |
/** | |
* Extension of timeout property as a class property | |
* @type {Number} | |
*/ | |
Object.defineProperty(BasicCachingStrategy, 'timeout', { | |
configurable: false, | |
enumerable: true, | |
writable: true, | |
value: BasicCachingStrategy.prototype.timeout | |
}); | |
/** | |
* Extension of parent class for error class to use if a timeout occurs | |
* @type {Error} | |
*/ | |
Object.defineProperty(BasicCachingStrategy, 'TimeoutError', { | |
configurable: false, | |
enumerable: true, | |
writable: true, | |
value: TimeoutError | |
}); | |
/** | |
* Default `always miss` caching function (should always be overridden) | |
* @param {String} key Key for cache | |
* @param {Function} callback Callback (err, cached?, cachedValue) | |
* @return {BasicCachingStrategy} | |
*/ | |
BasicCachingStrategy.prototype.get = function (key, callback) { | |
callback(null, false, null); | |
return this; | |
}; | |
/** | |
* Default `always not cached` caching function (should always be overridden) | |
* @param {String} key Key for cache | |
* @param {Mixed} value Value to store | |
* @param {Function} callback Callback (err, cached?) | |
* @return {BasicCachingStrategy} | |
*/ | |
BasicCachingStrategy.prototype.set = function (key, value, callback) { | |
callback(null, false); | |
return this; | |
}; | |
/** | |
* Wrapper method for `get` with the addition of a timeout | |
* | |
* If you are writing a library to use this object, you should always | |
* call the timeout version of the applicable function. Override at | |
* your own risk. | |
* | |
* @param {String} key Key for cache | |
* @param {Function} callback Callback (err, cached?, cachedValue) | |
* @return {BasicCachingStrategy} | |
*/ | |
BasicCachingStrategy.prototype.timeout_get = function (key, callback) { | |
var self = this, | |
timeout, | |
called = false; | |
timeout = setTimeout(function () { | |
called = true; | |
callback(new BasicCachingStrategy.TimeoutError('reached during get'), false, null); | |
}, this.timeout); | |
this.get(key, function () { | |
clearTimeout(timeout); | |
if (called) { return; } | |
callback.apply(self, arguments); | |
}); | |
return this; | |
}; | |
/** | |
* Wrapper method for `set` with the addition of a timeout | |
* | |
* If you are writing a library to use this object, you should always | |
* call the timeout version of the applicable function. Override at | |
* your own risk. | |
* | |
* @param {String} key Key for cache | |
* @param {Mixed} value Value to store | |
* @param {Function} callback Callback (err, cached?) | |
* @return {BasicCachingStrategy} | |
*/ | |
BasicCachingStrategy.prototype.timeout_set = function (key, value, callback) { | |
var self = this, | |
timeout, | |
called = false; | |
timeout = setTimeout(function () { | |
called = true; | |
callback(new BasicCachingStrategy.TimeoutError('reached during set'), false); | |
}, this.timeout); | |
this.set(key, value, function () { | |
clearTimeout(timeout); | |
if (called) { return; } | |
callback.apply(self, arguments); | |
}); | |
return this; | |
}; | |
// AMD / RequireJS | |
if (typeof define !== 'undefined' && define.amd) { | |
define([], function () { | |
return BasicCachingStrategy; | |
}); | |
} | |
// Node.js | |
else if (typeof module !== 'undefined' && module.exports) { | |
module.exports = BasicCachingStrategy; | |
} | |
// included directly via <script> tag | |
else { | |
this.BasicCachingStrategy = BasicCachingStrategy; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment