Skip to content
Myr

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

2 files+42 added−30 removed

modified:lib/express/plugins/cache.js+30 added-22 removed
24 unmodified lines
25 * 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 line
39 * 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 lines
79 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 lines
164 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 lines
modified:spec/spec.plugins.cache.js+12 added-8 removed