Finished memory data-store for cache api. Closes #130
Before adoption: this commit came in with the history the project adopted, so no landing records it.
expiration etc coming soon.
authored by visionmedia · committed by visionmedia · parent 965cbf6 · Browse files · 1 file·+107 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+107 added−18 removed
- other:+107 added−18 removed
Provenance
- Commit
4ce5c76- Tree
c217a67- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
965cbf6
1 file changed+107 added−18 removed
modified:lib/express/plugins/cache.js+107 added-18 removed
2 unmodified lines345678978910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697343536373898404142434445464799100491011021031041055410610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614713 unmodified lines2 unmodified lines
// --- Store
exports.Store = Class({ toString: function() { return '[' + this.name + ' Store]' }, /** * Ensure that the given _key_ and _val_ are strings. * Override in subclass to provide data-store specific functionality. * * @param {string} key * @param {string} val * @api public */ set: function(key, val) { if (typeof key !== 'string') throw new Error(this.name + ' store #set() key must be a string') if (typeof val !== 'string') throw new Error(this.name + ' store #set() value must be a string') }, /** * Ensure that the given _key_ is a string. * Override in subclass to provide data-store specific functionality. * * @param {string} key * @api public */ get: function(key) { if (typeof key !== 'string') throw new Error(this.name + 'store #get() key must be a string') }, /** * Convert to '[NAME Store]'. * * @return {string} * @api public */ toString: function() { return '[' + this.name + ' Store]' }})
// --- Store.Memory
exports.Store.Memory = exports.Store.extend({ /** * Datastore name. */ name: 'Memory', /** * Initialize data. */ init: function() { this.data = {} }, /** * Set the given _key_ to _val_, returning _val_. * * @param {string} key * @param {string} val * @return {string} * @api public */ set: function(key, val) { this.__super__(key, val) return this.data[key] = val }, /** * Get data found matching the given _key_. * * Examples: * * cache.get('page:front') * // => '<html>...</html>' * * cache.get('page:*') * // => { 'page:front': '<html>...</html>', * 'page:users': '<html>...</html>', * ... } * * @param {string} key * @return {mixed} * @api public */ get: function(key) { this.__super__(key) return key.indexOf('*') === -1 ? this.data[key] : this.search(key) }, clear: function(key) { if (key.indexOf('*') === -1) delete this.data[key] else key = this.normalize(key) }, normalize: function(pattern) { return new RegExp('^' + pattern.replace(/[*]/g, '(.*?)') + '$') }, search: function(pattern) { return this.data[key] var vals = {}, regexp = this.normalize(pattern) regexp = this.normalize(key) for (var key in this.data) if (this.data.hasOwnProperty(key)) if (regexp.test(key)) vals[key] = this.data[key] return vals return vals }, /** * Clear data matching the given _key_. * * Examples: * * cache.clear('page:front') * cache.clear('page:*') * * @param {string} key * @api public */ clear: function(key) { if (key.indexOf('*') === -1) return delete this.data[key] var regexp = this.normalize(key) for (var key in this.data) if (this.data.hasOwnProperty(key)) if (regexp.test(key)) delete this.data[key] }, /** * Convert the given key matching _pattern_ * into a RegExp. * * - * is converted to (.*?) * * @param {string} pattern * @return {regexp} * @api private */ normalize: function(pattern) { return new RegExp('^' + pattern.replace(/[*]/g, '(.*?)') + '$') }})
exports.Cache = Plugin.extend({13 unmodified lines