Skip to content
Myr

cache.js

This file

Path

lib/express/plugins/cache.js

234 lines · 4.9 KiB · JavaScript

Last changed by

Utilizing Date.now()visionmediabefore adoption3cc7b4d

History of this file

Take it

The walk stopped after 200 commits: lines older than 315c050 point at it.

Merge branch 'abstract-session'visionmediabefore adoption315c050

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

Merge branch 'globals'visionmediabefore adoption8b58732

lines 4–9why, one step back
4/**
5 * Module dependencies.6 */7 8var Request = require('express/request').Request9

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 10–11why, one step back
10// --- Cache
11

Merge branch 'class'visionmediabefore adoption2aa858b

line 12why, one step back
12var Cache = new Class({

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 13–17why, one step back
13
14 /**15 * Initialize cache with _key_ and _val_.16 */17

Merge branch 'class'visionmediabefore adoption2aa858b

line 18why, one step back
18 constructor: function(key, val) {

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 19–20why, one step back
19 this.key = key
20 this.val = val

Utilizing Date.now()visionmediabefore adoption3cc7b4d

line 21why, one step back
21 this.created = Date.now()

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 22–24why, one step back
22 }
23})24

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 25–26why, one step back
25// --- Store
26

Merge branch 'class'visionmediabefore adoption2aa858b

line 27why, one step back
27exports.Store = new Class({

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 28–34why, one step back
28
29 /**30 * Ensure that the given _key_ is a string.31 * Override in subclass to provide data-store specific functionality.32 *33 * @param {string} key34 * @param {string} val

Merge branch 'integration'visionmediabefore adoptionf8e4333

line 35why, one step back
35 * @param {function} callback

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 36–38why, one step back
36 * @api public
37 */38

Merge branch 'integration'visionmediabefore adoptionf8e4333

line 39why, one step back
39 set: function(key, val, callback) {

Merge branch 'ext'visionmediabefore adoption364c131

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

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 42–48why, one step back
42 },
43 44 /**45 * Ensure that the given _key_ is a string.46 * Override in subclass to provide data-store specific functionality.47 *48 * @param {string} key

Merge branch 'integration'visionmediabefore adoptionf8e4333

line 49why, one step back
49 * @param {function} callback

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 50–52why, one step back
50 * @api public
51 */52

Merge branch 'integration'visionmediabefore adoptionf8e4333

line 53why, one step back
53 get: function(key, callback) {

Merge branch 'ext'visionmediabefore adoption364c131

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

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 56–83why, one step back
56 },
57 58 /**59 * Convert to '[NAME Store]'.60 *61 * @return {string}62 * @api public63 */64 65 toString: function() {66 return '[' + this.name + ' Store]'67 }68})6970// --- Store.Memory7172exports.Store.Memory = exports.Store.extend({73 74 /**75 * Datastore name.76 */77 78 name: 'Memory',79 80 /**81 * Initialize data.82 */83

Merge branch 'class'visionmediabefore adoption2aa858b

line 84why, one step back
84 constructor: function() {

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 85–88why, one step back
85 this.data = {}
86 },87 88 /**

Merge branch 'async-cache'visionmediabefore adoption7606f1b

line 89why, one step back
89 * Set the given _key_ to _val_.

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 90–92why, one step back
90 *
91 * @param {string} key92 * @param {string} val

Merge branch 'integration'visionmediabefore adoption33443d9

line 93why, one step back
93 * @param {function} callback

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 94–97why, one step back
94 * @return {string}
95 * @api public96 */97

Merge branch 'integration'visionmediabefore adoption33443d9

line 98why, one step back
98 set: function(key, val, callback) {

Merge branch 'class'visionmediabefore adoption2aa858b

line 99why, one step back
99 exports.Store.prototype.set.apply(this, arguments)

Merge branch 'async-cache'visionmediabefore adoption7606f1b

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

Merge branch 'integration'visionmediabefore adoption33443d9

line 101why, one step back
101 if (callback) callback(val)

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 102–108why, one step back
102 },
103 104 /**105 * Get data found matching the given _key_.106 *107 * Examples:108 *

Merge branch 'async-cache'visionmediabefore adoption7606f1b

line 109why, one step back
109 * cache.get('page:front', function(val){})

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 110–111why, one step back
110 * // => '<html>...</html>'
111 *

Merge branch 'async-cache'visionmediabefore adoption7606f1b

line 112why, one step back
112 * cache.get('page:*', function(vals){})

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 113–117why, one step back
113 * // => { 'page:front': '<html>...</html>',
114 * 'page:users': '<html>...</html>',115 * ... }116 *117 * @param {string} key

Merge branch 'integration'visionmediabefore adoption33443d9

line 118why, one step back
118 * @param {function} callback

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 119–122why, one step back
119 * @return {mixed}
120 * @api public121 */122

Merge branch 'integration'visionmediabefore adoption33443d9

line 123why, one step back
123 get: function(key, callback) {

Merge branch 'class'visionmediabefore adoption2aa858b

line 124why, one step back
124 exports.Store.prototype.get.apply(this, arguments)

Merge branch 'abstract-session'visionmediabefore adoption315c050

line 125why, one step back
125 if (key.indexOf('*') === -1)

Merge branch 'integration'visionmediabefore adoption33443d9

line 126why, one step back
126 return callback(this.data[key] instanceof Cache

Fixed ternary stylevisionmediabefore adoption1c514df

line 127why, one step back
127 ? this.data[key].val

Merge branch 'async-cache'visionmediabefore adoption7606f1b

line 128why, one step back
128 : null)

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

line 129why, one step back
129 var regexp = this.normalize(key)

Merge branch 'integration'visionmediabefore adoption33443d9

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

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 131–133why, one step back
131 if (regexp.test(cache.key))
132 vals[cache.key] = cache.val133 return vals

Merge branch 'async-cache'visionmediabefore adoption7606f1b

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

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 135–141why, one step back
135 },
136 137 /**138 * Clear data matching the given _key_.139 *140 * Examples:141 *

Merge branch 'async-cache'visionmediabefore adoption7606f1b

lines 142–143why, one step back
142 * cache.clear('page:front', function(){})
143 * cache.clear('page:*', function(){})

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 144–145why, one step back
144 *
145 * @param {string} key

Merge branch 'integration'visionmediabefore adoption33443d9

line 146why, one step back
146 * @param {function} callback

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 147–149why, one step back
147 * @api public
148 */149

Merge branch 'integration'visionmediabefore adoption33443d9

line 150why, one step back
150 clear: function(key, callback) {

Merge branch 'abstract-session'visionmediabefore adoption315c050

line 151why, one step back
151 if (key.indexOf('*') === -1)

Merge branch 'async-cache'visionmediabefore adoption7606f1b

lines 152–159why, one step back
152 delete this.data[key]
153 else {154 var regexp = this.normalize(key)155 for (var key in this.data)156 if (this.data.hasOwnProperty(key))157 if (regexp.test(key))158 delete this.data[key]159 }

Merge branch 'integration'visionmediabefore adoption33443d9

line 160why, one step back
160 callback()

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 161–163why, one step back
161 },
162 163 /**

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 164–169why, one step back
164 * Reap caches older than _ms_.
165 *166 * @param {int} ms167 * @api private168 */169

Merge branch 'integration'visionmediabefore adoption33443d9

line 170why, one step back
170 reap: function(ms, callback) {

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

line 171why, one step back
171 var self = this,

Utilizing Date.now()visionmediabefore adoption3cc7b4d

line 172why, one step back
172 threshold = +new Date(Date.now() - ms)

Merge branch 'ext'visionmediabefore adoption364c131

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

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

line 174why, one step back
174 if (cache.created < threshold)

Merge branch 'async-cache'visionmediabefore adoption7606f1b

line 175why, one step back
175 self.clear(cache.key, function(){})

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

line 176why, one step back
176 })

Merge branch 'integration'visionmediabefore adoption33443d9

line 177why, one step back
177 if (callback) callback()

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 178–180why, one step back
178 },
179 180 /**

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 181–195why, one step back
181 * Convert the given key matching _pattern_
182 * into a RegExp.183 *184 * - * is converted to (.*?)185 *186 * @param {string} pattern187 * @return {regexp}188 * @api private189 */190 191 normalize: function(pattern) {192 return new RegExp('^' + pattern.replace(/[*]/g, '(.*?)') + '$')193 }194})195

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 196–197why, one step back
196// --- Cache
197

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 198–201why, one step back
198exports.Cache = Plugin.extend({
199 extend: {200 201 /**

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 202–205why, one step back
202 * Initialize memory store and start reaper.
203 *204 * Options:205 *

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

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

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 209–211why, one step back
209 *
210 * @param {hash} options211 * @api private

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 212–213why, one step back
212 */
213

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

line 214why, one step back
214 init: function(options) {

Removed process.mixin() dependencyvisionmediabefore adoption247e6ad

line 215why, one step back
215 this.merge(options || {})

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 216–223why, one step back
216 this.store = new (this.dataStore || exports.Store.Memory)(options)
217 Request.include({ cache: this.store })218 this.startReaper()219 },220 221 /**222 * Start reaper.223 *

Merge branch 'integration'visionmediabefore adoption33443d9

line 224why, one step back
224 * @param {function} callback

Merge branch 'cache-lifetime'visionmediabefore adoption5ce1306

lines 225–227why, one step back
225 * @api private
226 */227

Merge branch 'integration'visionmediabefore adoption33443d9

line 228why, one step back
228 startReaper: function(callbac) {

Closes #52visionmediabefore adoption161c9f9

lines 229–231why, one step back
229 setInterval(function(self){
230 self.store.reap(self.lifetime || (1).day)231 }, this.reapInterval || this.reapEvery || (1).hour, this)

Merge branch 'abstract-session'visionmediabefore adoption315c050

lines 232–234why, one step back
232 }
233 }234})