Skip to content
Myr

cache.js

This file

Path

lib/express/plugins/cache.js

220 lines · 4.4 KiB · JavaScript

Last changed by

Merge branch 'ext'visionmediabefore adoption364c131

History of this file

Take it

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 1–3why, one step back
1
2// Express - Cache - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 4–18why, one step back
4// --- Cache
56var Cache = Class({7 8 /**9 * Initialize cache with _key_ and _val_.10 */11 12 init: function(key, val) {13 this.key = key14 this.val = val15 this.created = Number(new Date)16 }17})18

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 19–32why, one step back
19// --- Store
2021exports.Store = Class({22 23 /**24 * Ensure that the given _key_ is a string.25 * Override in subclass to provide data-store specific functionality.26 *27 * @param {string} key28 * @param {string} val29 * @api public30 */31 32 set: function(key, val) {

Merge branch 'ext'visionmediabefore adoption364c131

lines 33–34why, one step back
33 if (typeof key !== 'string')
34 throw new Error(this.name + ' store #set() key must be a string')

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 35–45why, one step back
35 },
36 37 /**38 * Ensure that the given _key_ is a string.39 * Override in subclass to provide data-store specific functionality.40 *41 * @param {string} key42 * @api public43 */44 45 get: function(key) {

Merge branch 'ext'visionmediabefore adoption364c131

lines 46–47why, one step back
46 if (typeof key !== 'string')
47 throw new Error(this.name + 'store #get() key must be a string')

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 48–90why, one step back
48 },
49 50 /**51 * Convert to '[NAME Store]'.52 *53 * @return {string}54 * @api public55 */56 57 toString: function() {58 return '[' + this.name + ' Store]'59 }60})6162// --- Store.Memory6364exports.Store.Memory = exports.Store.extend({65 66 /**67 * Datastore name.68 */69 70 name: 'Memory',71 72 /**73 * Initialize data.74 */75 76 init: function() {77 this.data = {}78 },79 80 /**81 * Set the given _key_ to _val_, returning _val_.82 *83 * @param {string} key84 * @param {string} val85 * @return {string}86 * @api public87 */88 89 set: function(key, val) {90 this.__super__(key, val)

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

line 91why, one step back
91 return this.data[key] = new Cache(key, val), val

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 92–114why, one step back
92 },
93 94 /**95 * Get data found matching the given _key_.96 *97 * Examples:98 *99 * cache.get('page:front')100 * // => '<html>...</html>'101 *102 * cache.get('page:*')103 * // => { 'page:front': '<html>...</html>', 104 * 'page:users': '<html>...</html>',105 * ... }106 *107 * @param {string} key108 * @return {mixed}109 * @api public110 */111 112 get: function(key) {113 this.__super__(key)114 if (key.indexOf('*') === -1)

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 115–118why, one step back
115 return this.data[key] instanceof Cache ?
116 this.data[key].val :117 null118 var regexp = this.normalize(key)

Merge branch 'ext'visionmediabefore adoption364c131

line 119why, one step back
119 return this.data.reduce(function(vals, cache){

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 120–122why, one step back
120 if (regexp.test(cache.key))
121 vals[cache.key] = cache.val122 return vals

Merge branch 'ext'visionmediabefore adoption364c131

line 123why, one step back
123 }, {})

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 124–148why, one step back
124 },
125 126 /**127 * Clear data matching the given _key_.128 *129 * Examples:130 *131 * cache.clear('page:front')132 * cache.clear('page:*')133 *134 * @param {string} key135 * @api public136 */137 138 clear: function(key) {139 if (key.indexOf('*') === -1)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 },147 148 /**

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 149–157why, one step back
149 * Reap caches older than _ms_.
150 *151 * @param {int} ms152 * @api private153 */154 155 reap: function(ms) {156 var self = this,157 threshold = Number(new Date(Number(new Date) - ms))

Merge branch 'ext'visionmediabefore adoption364c131

line 158why, one step back
158 this.data.each(function(cache){

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 159–164why, one step back
159 if (cache.created < threshold)
160 self.clear(cache.key)161 })162 },163 164 /**

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 165–179why, one step back
165 * Convert the given key matching _pattern_
166 * into a RegExp.167 *168 * - * is converted to (.*?)169 *170 * @param {string} pattern171 * @return {regexp}172 * @api private173 */174 175 normalize: function(pattern) {176 return new RegExp('^' + pattern.replace(/[*]/g, '(.*?)') + '$')177 }178})179

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 180–181why, one step back
180// --- Cache
181

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 182–185why, one step back
182exports.Cache = Plugin.extend({
183 extend: {184 185 /**

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 186–189why, one step back
186 * Initialize memory store and start reaper.
187 *188 * Options:189 *

Both Cache and Session option "reapInterval" may be "reapEvery". Closes #174visionmediabefore adoption9efb15b

lines 190–192why, one step back
190 * - dataStore constructor name of cache data store, defaults to Store.Memory
191 * - lifetime lifetime of cache in milliseconds, defaults to one day192 * - reapInterval, reapEvery interval in milliseconds in which to reap old caches, defaults to one hour

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 193–195why, one step back
193 *
194 * @param {hash} options195 * @api private

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 196–197why, one step back
196 */
197

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 198–216why, one step back
198 init: function(options) {
199 process.mixin(this, options)200 this.store = new (this.dataStore || exports.Store.Memory)(options)201 Request.include({ cache: this.store })202 this.startReaper()203 },204 205 /**206 * Start reaper.207 *208 * @api private209 */210 211 startReaper: function() {212 var self = this,213 oneDay = 86400000,214 oneHour = 3600000215 setInterval(function(){216 self.store.reap(self.lifetime || oneDay)

Both Cache and Session option "reapInterval" may be "reapEvery". Closes #174visionmediabefore adoption9efb15b

line 217why, one step back
217 }, self.reapInterval || self.reapEvery || oneHour)

Merge branch 'master' of git://github.com/visionmedia/expressciaranjbefore adoption704c933

lines 218–220why, one step back
218 }
219 }220})