Cache api is now async
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent b92f1b7 · Browse files · 2 files·+42 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+42 added−30 removed
- tests:+12 added−8 removed
- other:+30 added−22 removed
Provenance
- Commit
b85cda3- Tree
46ece25- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
b92f1b7
2 files changed+42 added−30 removed
modified:lib/express/plugins/cache.js+30 added-22 removed
24 unmodified lines25 * Override in subclass to provide data-store specific functionality.26 *27 * @param {string} key28 * @param {string} val29+ * @param {function} fn30 * @api public31 */32 32- set: function(key, val) {33+ set: function(key, val, fn) {34 if (typeof key !== 'string')35 throw new Error(this.name + ' store #set() key must be a string')36 },37 1 unmodified line39 * Ensure that the given _key_ is a string.40 * Override in subclass to provide data-store specific functionality.41 *42 * @param {string} key43+ * @param {function} fn44 * @api public45 */46 45- get: function(key) {47+ get: function(key, fn) {48 if (typeof key !== 'string')49 throw new Error(this.name + 'store #get() key must be a string')50 },51 27 unmodified lines79 this.data = {}80 },81 82 /**81- * Set the given _key_ to _val_, returning _val_.83+ * Set the given _key_ to _val_, passing _val_.84 *85 * @param {string} key86 * @param {string} val87+ * @param {function} fn88 * @return {string}89 * @api public90 */91 89- set: function(key, val) {92+ set: function(key, val, fn) {93 exports.Store.prototype.set.apply(this, arguments)91- return this.data[key] = new Cache(key, val), val94+ return this.data[key] = new Cache(key, val), fn(val)95 },96 97 /**98 * Get data found matching the given _key_.99 *100 * Examples:101 *99- * cache.get('page:front')102+ * cache.get('page:front', function(val){})103 * // => '<html>...</html>'104 *102- * cache.get('page:*')105+ * cache.get('page:*', function(vals){})106 * // => { 'page:front': '<html>...</html>', 107 * 'page:users': '<html>...</html>',108 * ... }109 *110 * @param {string} key111+ * @param {function} fn112 * @return {mixed}113 * @api public114 */115 112- get: function(key) {116+ get: function(key, fn) {117 exports.Store.prototype.get.apply(this, arguments)118 if (key.indexOf('*') === -1)115- return this.data[key] instanceof Cache119+ return fn(this.data[key] instanceof Cache120 ? this.data[key].val117- : null121+ : null)122 var regexp = this.normalize(key) 119- return this.data.reduce(function(vals, cache){123+ fn(this.data.reduce(function(vals, cache){124 if (regexp.test(cache.key))125 vals[cache.key] = cache.val126 return vals123- }, {})127+ }, {}))128 },129 130 /**131 * Clear data matching the given _key_.132 *133 * Examples:134 *131- * cache.clear('page:front')132- * cache.clear('page:*')135+ * cache.clear('page:front', function(){})136+ * cache.clear('page:*', function(){})137 *138 * @param {string} key139+ * @param {function} fn140 * @api public141 */142 138- clear: function(key) {143+ clear: function(key, fn) {144 if (key.indexOf('*') === -1)145+ delete this.data[key]140- return delete this.data[key]141- var regexp = this.normalize(key)142- for (var key in this.data)143- if (this.data.hasOwnProperty(key))144- if (regexp.test(key))145- delete this.data[key]146+ else {147+ var regexp = this.normalize(key)148+ for (var key in this.data)149+ if (this.data.hasOwnProperty(key))150+ if (regexp.test(key))151+ delete this.data[key]152+ }153+ fn()154 },155 156 /**157 * Reap caches older than _ms_.6 unmodified lines164 var self = this,165 threshold = Number(new Date(Number(new Date) - ms))166 this.data.each(function(cache){167 if (cache.created < threshold)160- self.clear(cache.key)168+ self.clear(cache.key, function(){})169 })170 },171 172 /**53 unmodified linesmodified:spec/spec.plugins.cache.js+12 added-8 removed
130 unmodified linesExpand all131132133134135135136137138139140141142143144145146147148142143144145146147148149150151152153154155156130 unmodified linesExpand all }) }) }) }) results.should.eql [null, nukk] results.should.eql [null, null] end end end describe '#reap()' it 'should destroy caches older than the given age in milliseconds' store.set('user:one', '1') store.data['user:one'].created = Number(new Date) - 300 store.set('user:two', '2') store.data['user:two'].created = Number(new Date) - 100 store.reap(200) store.get('user:one').should.be_null store.get('user:two').should.not.be_null store.set('user:one', '1', function(){}) store.data['user:one'].created = Number((5).minutes.ago) store.set('user:two', '2', function(){}) store.data['user:two'].created = Number((2).seconds.ago) store.reap((1).minute) store.get('user:one', function(val){ val.should.be_null }) store.get('user:two', function(val){ val.should.not.be_null }) end end endendNo newline at end of fileNo newline at end of file