This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-cache.js
182 lines (177 loc) · 5.05 KB
/
http-cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com, Anthi Oikonomou anthioikonomou@gmail.com
*
* Released under the BSD3-Clause license
* Date: 2014-03-10
*/
/**
* @ignore
*/
var dat = require('most-data'), util = require('util');
/**
* Implements the cache for a data application.
* @class HttpCache
* @param {{ttl:number}|*} options
* @constructor
* @augments EventEmitter2
*/
function HttpCache(options) {
this.initialized = false;
options = options || {};
options.ttl = options.ttl || (20*60);
this.options = options;
}
util.inherits(HttpCache, dat.types.EventEmitter2);
/**
* Initializes data caching.
* @param {function(Error=)} callback
*/
HttpCache.prototype.init = function(callback) {
try {
if (this.initialized) {
callback();
return;
}
var NodeCache = require( "node-cache" );
this.rawCache = new NodeCache();
this.initialized = true;
callback();
}
catch (e) {
callback(e);
}
};
/**
* Removes a cached value.
* @param {string} key - A string that represents the key of the cached value
* @param {function(Error=,number=)} callback - Returns the number of deleted entries. This parameter is optional.
*/
HttpCache.prototype.remove = function(key, callback) {
var self = this;
callback = callback || function() {};
self.init(function(err) {
if (err) {
callback(err);
}
else {
self.rawCache.set(key, callback);
}
});
};
/**
* Flush all cached data.
* @param {function(Error=)} callback - This parameter is optional.
*/
HttpCache.prototype.removeAll = function(callback) {
var self = this;
callback = callback || function() {};
self.init(function(err) {
if (err) {
callback(err);
}
else {
self.rawCache.flushAll();
callback();
}
});
};
/**
* Sets a key value pair in cache.
* @param {string} key - A string that represents the key of the cached value
* @param {*} value - The value to be cached
* @param {number=} ttl - A TTL in seconds. This parameter is optional.
* @param {function(Error=,boolean=)} callback - Returns true on success. This parameter is optional.
*/
HttpCache.prototype.add = function(key, value, ttl, callback) {
var self = this;
if (typeof ttl === 'undefined')
ttl = self.options.ttl;
callback = callback || function() {};
self.init(function(err) {
if (err) {
callback(err);
}
else {
if (typeof value !== 'undefined')
self.rawCache.set(key, value, ttl, callback);
else
callback(null, false);
}
});
};
/**
* Gets data from cache or executes the defined function and adds the result to the cache with the specified key
* @param {string|*} key - A string thath represents the of the cached data
* @param {function(function(Error=,*=))} fn - A function to execute if data will not be found in cache
* @param {function(Error=,*=)} callback - A callback function that will return the result or an error, if any.
*/
HttpCache.prototype.ensure = function(key, fn, callback) {
var self = this;
callback = callback || function() {};
if (typeof fn !== 'function') {
callback(new Error('Invalid argument. Expected function.'));
return;
}
//try to get from cache
self.get(key, function(err, result) {
if (err) { callback(err); return; }
if (typeof result !== 'undefined') {
callback(null, result);
}
else {
//execute fn
fn(function(err, result) {
if (err) { callback(err); return; }
//add to cache
self.add(key, (typeof result === 'undefined') ? null: result, self.options.ttl, function() {
//and return result
callback(null, result);
});
});
}
});
};
/**
* Gets a cached value defined by the given key.
* @param {string|*} key
* @param {function(Error=,*=)} callback - A callback that returns the cached value, if any.
*/
HttpCache.prototype.get = function(key, callback) {
var self = this;
callback = callback || function() {};
if (typeof key === 'undefined' || key == null) {
callback();
}
self.init(function(err) {
if (err) {
callback(err);
}
else {
self.rawCache.get(key, function(err, value) {
if (err) {
callback(err);
}
else {
if (typeof value[key] !== 'undefined') {
callback(null, value[key]);
}
else {
callback();
}
}
});
}
});
};
if (typeof exports !== 'undefined')
{
/**
* @see HttpCache
* @constructs HttpCache
*/
module.exports = HttpCache;
}