cloudcms-server 3.2.285 → 3.2.287
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/broadcast/broadcast.js +8 -5
- package/locks/locks.js +6 -4
- package/middleware/awareness/awareness.js +6 -4
- package/middleware/cache/cache.js +6 -4
- package/middleware/cache/providers/shared-memory.js +3 -3
- package/package.json +3 -2
- package/util/auth.js +6 -4
- package/util/proxy-factory.js +6 -6
- package/temp/memored/.jshintrc +0 -4
- package/temp/memored/README.md +0 -240
- package/temp/memored/demo/demo1.js +0 -37
- package/temp/memored/demo/demo2.js +0 -32
- package/temp/memored/gulpfile.js +0 -8
- package/temp/memored/index.js +0 -343
- package/temp/memored/package.json +0 -54
- package/temp/memored/spec/memored.spec.js +0 -265
package/broadcast/broadcast.js
CHANGED
|
@@ -11,12 +11,15 @@ module.exports = function()
|
|
|
11
11
|
var self = this;
|
|
12
12
|
|
|
13
13
|
// set up defaults
|
|
14
|
-
if (!process.env.CLOUDCMS_BROADCAST_TYPE)
|
|
14
|
+
if (!process.env.CLOUDCMS_BROADCAST_TYPE)
|
|
15
|
+
{
|
|
15
16
|
process.env.CLOUDCMS_BROADCAST_TYPE = "local";
|
|
16
|
-
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
|
|
18
|
+
// auto-configure
|
|
19
|
+
if (process.env.CLOUDCMS_LAUNCHPAD_SETUP === "redis")
|
|
20
|
+
{
|
|
21
|
+
process.env.CLOUDCMS_BROADCAST_TYPE = "redis";
|
|
22
|
+
}
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
var config = process.configuration;
|
package/locks/locks.js
CHANGED
|
@@ -39,10 +39,12 @@ exports = module.exports = function()
|
|
|
39
39
|
if (!process.env.CLOUDCMS_LOCKS_TYPE)
|
|
40
40
|
{
|
|
41
41
|
process.env.CLOUDCMS_LOCKS_TYPE = "memory";
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
|
|
43
|
+
// auto-configure
|
|
44
|
+
if (process.env.CLOUDCMS_LAUNCHPAD_SETUP === "redis")
|
|
45
|
+
{
|
|
46
|
+
process.env.CLOUDCMS_LOCKS_TYPE = "redis";
|
|
47
|
+
}
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
if (!process.configuration.locks) {
|
|
@@ -43,10 +43,12 @@ exports = module.exports = function()
|
|
|
43
43
|
if (!process.env.CLOUDCMS_AWARENESS_TYPE)
|
|
44
44
|
{
|
|
45
45
|
process.env.CLOUDCMS_AWARENESS_TYPE = "memory";
|
|
46
|
-
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
|
|
47
|
+
// auto-configure
|
|
48
|
+
if (process.env.CLOUDCMS_LAUNCHPAD_SETUP === "redis")
|
|
49
|
+
{
|
|
50
|
+
process.env.CLOUDCMS_AWARENESS_TYPE = "redis";
|
|
51
|
+
}
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
if (!process.configuration.awareness) {
|
|
@@ -27,10 +27,12 @@ exports = module.exports = function()
|
|
|
27
27
|
if (!process.env.CLOUDCMS_CACHE_TYPE) {
|
|
28
28
|
|
|
29
29
|
process.env.CLOUDCMS_CACHE_TYPE = "memory";
|
|
30
|
-
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
|
|
31
|
+
// auto-configure
|
|
32
|
+
if (process.env.CLOUDCMS_LAUNCHPAD_SETUP === "redis")
|
|
33
|
+
{
|
|
34
|
+
process.env.CLOUDCMS_CACHE_TYPE = "redis";
|
|
35
|
+
}
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
if (!process.configuration.cache.type)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var path = require("path");
|
|
2
|
-
var memored = require(
|
|
1
|
+
//var path = require("path");
|
|
2
|
+
var memored = require("memored");
|
|
3
3
|
|
|
4
|
-
var cluster = require("cluster");
|
|
4
|
+
//var cluster = require("cluster");
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Shared cluster memory using memored
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
},
|
|
7
7
|
"name": "cloudcms-server",
|
|
8
8
|
"description": "Cloud CMS Application Server Module",
|
|
9
|
-
"version": "3.2.
|
|
9
|
+
"version": "3.2.287",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "git://github.com/gitana/cloudcms-server.git"
|
|
@@ -50,8 +50,9 @@
|
|
|
50
50
|
"json5": "^1.0.1",
|
|
51
51
|
"jsonwebtoken": "^8.5.1",
|
|
52
52
|
"klaw": "^1.3.1",
|
|
53
|
-
"lru-cache": "^
|
|
53
|
+
"lru-cache": "^7.14.0",
|
|
54
54
|
"marked": "^4.0.14",
|
|
55
|
+
"memored": "^1.1.1",
|
|
55
56
|
"memorystore": "^1.6.1",
|
|
56
57
|
"mime": "^1.6.0",
|
|
57
58
|
"mkdirp": "^0.5.1",
|
package/util/auth.js
CHANGED
|
@@ -8,16 +8,18 @@ var http = require("http");
|
|
|
8
8
|
var https = require("https");
|
|
9
9
|
var async = require("async");
|
|
10
10
|
|
|
11
|
+
var LRU = require("lru-cache");
|
|
12
|
+
|
|
11
13
|
// trusted profile cache size 100
|
|
12
|
-
var TRUSTED_PROFILE_CACHE =
|
|
14
|
+
var TRUSTED_PROFILE_CACHE = new LRU({
|
|
13
15
|
max:100,
|
|
14
|
-
|
|
16
|
+
ttl: 1000 * 60 * 15 // 15 minutes
|
|
15
17
|
});
|
|
16
18
|
|
|
17
19
|
// user entry cache size 100
|
|
18
|
-
var USER_ENTRY_CACHE =
|
|
20
|
+
var USER_ENTRY_CACHE = new LRU({
|
|
19
21
|
max: 100,
|
|
20
|
-
|
|
22
|
+
ttl: 1000 * 60 * 15 // 15 minutes
|
|
21
23
|
});
|
|
22
24
|
|
|
23
25
|
var Gitana = require("gitana");
|
package/util/proxy-factory.js
CHANGED
|
@@ -10,6 +10,8 @@ var oauth2 = require("./oauth2")();
|
|
|
10
10
|
var urlTool = require("url");
|
|
11
11
|
const finalhandler = require("finalhandler");
|
|
12
12
|
|
|
13
|
+
var LRU = require("lru-cache");
|
|
14
|
+
|
|
13
15
|
var exports = module.exports;
|
|
14
16
|
|
|
15
17
|
var _LOCK = function(lockIdentifiers, workFunction)
|
|
@@ -18,9 +20,9 @@ var _LOCK = function(lockIdentifiers, workFunction)
|
|
|
18
20
|
process.locks.lock(name, workFunction);
|
|
19
21
|
};
|
|
20
22
|
|
|
21
|
-
var NAMED_PROXY_HANDLERS_CACHE =
|
|
23
|
+
var NAMED_PROXY_HANDLERS_CACHE = new LRU({
|
|
22
24
|
max: 200,
|
|
23
|
-
|
|
25
|
+
ttl: 1000 * 60 * 60 // 60 minutes
|
|
24
26
|
});
|
|
25
27
|
|
|
26
28
|
var acquireProxyHandler = exports.acquireProxyHandler = function(proxyTarget, pathPrefix, callback)
|
|
@@ -30,7 +32,6 @@ var acquireProxyHandler = exports.acquireProxyHandler = function(proxyTarget, pa
|
|
|
30
32
|
// is it already in LRU cache?
|
|
31
33
|
// if so hand it back
|
|
32
34
|
var _cachedHandler = NAMED_PROXY_HANDLERS_CACHE[name];
|
|
33
|
-
_cachedHandler = null;
|
|
34
35
|
if (_cachedHandler)
|
|
35
36
|
{
|
|
36
37
|
return callback(null, _cachedHandler);
|
|
@@ -49,7 +50,6 @@ var acquireProxyHandler = exports.acquireProxyHandler = function(proxyTarget, pa
|
|
|
49
50
|
|
|
50
51
|
// second check to make sure another thread didn't create the handler in the meantime
|
|
51
52
|
_cachedHandler = NAMED_PROXY_HANDLERS_CACHE[name];
|
|
52
|
-
_cachedHandler = null;
|
|
53
53
|
if (_cachedHandler)
|
|
54
54
|
{
|
|
55
55
|
releaseLockFn();
|
|
@@ -57,7 +57,7 @@ var acquireProxyHandler = exports.acquireProxyHandler = function(proxyTarget, pa
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
// create the proxy handler and cache it into LRU cache
|
|
60
|
-
console.log("Acquiring proxy handler: " + name + ", for target: " + proxyTarget + " and prefix: " + pathPrefix);
|
|
60
|
+
//console.log("Acquiring proxy handler: " + name + ", for target: " + proxyTarget + " and prefix: " + pathPrefix);
|
|
61
61
|
_cachedHandler = createProxyHandler(proxyTarget, pathPrefix);
|
|
62
62
|
|
|
63
63
|
// store back into LRU cache
|
|
@@ -274,8 +274,8 @@ var createProxyHandler = function(proxyTarget, pathPrefix)
|
|
|
274
274
|
});
|
|
275
275
|
};
|
|
276
276
|
|
|
277
|
-
|
|
278
277
|
// cookie domain rewrite?
|
|
278
|
+
// not needed - this is handled intrinsically by http2-proxy
|
|
279
279
|
|
|
280
280
|
return proxyRequestHandler;
|
|
281
281
|
};
|
package/temp/memored/.jshintrc
DELETED
package/temp/memored/README.md
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
# Memored
|
|
2
|
-
|
|
3
|
-
Memored implements an in-memory shared cache to use in nodejs applications which uses [cluster](http://nodejs.org/api/cluster.html) module.
|
|
4
|
-
|
|
5
|
-
Let's say you want your application to take advantage of multi-core CPUs using nodejs cluster module; you will be able to run several _isolated_ processes which shared nothing but a communication channel with parent process.
|
|
6
|
-
If you need a fast volatile cache, common solutions would create an in-memory map for every process you run, so you end up with the same data stored several times in your machine RAM.
|
|
7
|
-
|
|
8
|
-
Memored uses communication channel between master process and its workers to use a unique in-memory storage, reducing the amount of memory your application would use.
|
|
9
|
-
|
|
10
|
-
## Getting Started
|
|
11
|
-
Install this module with _npm_:
|
|
12
|
-
```javascript
|
|
13
|
-
npm install memored
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
Store and read values is straightforward:
|
|
17
|
-
```javascript
|
|
18
|
-
var cluster = require('cluster'),
|
|
19
|
-
memored = require('memored');
|
|
20
|
-
|
|
21
|
-
if (cluster.isMaster) {
|
|
22
|
-
cluster.fork();
|
|
23
|
-
} else {
|
|
24
|
-
var han = {
|
|
25
|
-
firstname: 'Han',
|
|
26
|
-
lastname: 'Solo'
|
|
27
|
-
},
|
|
28
|
-
luke = {
|
|
29
|
-
firstname: 'Luke',
|
|
30
|
-
lastname: 'Skywalker'
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// Store and read
|
|
34
|
-
memored.store('character1', han, function() {
|
|
35
|
-
console.log('Value stored!');
|
|
36
|
-
|
|
37
|
-
memored.read('character1', function(err, value) {
|
|
38
|
-
console.log('Read value:', value);
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// You can also set a ttl (milliseconds)
|
|
43
|
-
memored.store('character2', luke, 1000, function(err, expirationTime) {
|
|
44
|
-
console.log('Value stored until:', new Date(expirationTime));
|
|
45
|
-
|
|
46
|
-
setTimeout(function() {
|
|
47
|
-
memored.read('character2', function(err, value) {
|
|
48
|
-
console.log('Value is gone?', value === undefined);
|
|
49
|
-
|
|
50
|
-
process.exit();
|
|
51
|
-
});
|
|
52
|
-
}, 1050);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## Invalidation management
|
|
58
|
-
By default, _memored_ will evict cache entries (stored with _ttl_) passively. This is, when you read an expired entry, you will get no value on return and _memored_ will delete the value from its internal cache.
|
|
59
|
-
|
|
60
|
-
You can also configure _memored_ to actively evict expired entries every N milliseconds. For this to work, you need to pass the attribute *purgeInterval* to the *setup* function.
|
|
61
|
-
This will trigger an internal function which looks for expired entries and deletes them from its internal cache.
|
|
62
|
-
|
|
63
|
-
Example:
|
|
64
|
-
```javascript
|
|
65
|
-
var cluster = require('cluster'),
|
|
66
|
-
async = require('async'),
|
|
67
|
-
memored = require('memored');
|
|
68
|
-
|
|
69
|
-
if (cluster.isMaster) {
|
|
70
|
-
|
|
71
|
-
cluster.fork();
|
|
72
|
-
memored.setup({ purgeInterval: 500});
|
|
73
|
-
|
|
74
|
-
} else {
|
|
75
|
-
|
|
76
|
-
async.series({
|
|
77
|
-
storeValue: function(next) {
|
|
78
|
-
memored.store('key1', 'My simple string value', 100, next);
|
|
79
|
-
},
|
|
80
|
-
readCacheSize: function(next) {
|
|
81
|
-
memored.size(function(err, size) {
|
|
82
|
-
console.log('Current size is 1?', size === 1);
|
|
83
|
-
next();
|
|
84
|
-
});
|
|
85
|
-
},
|
|
86
|
-
wait: function(next) {
|
|
87
|
-
setTimeout(next, 600);
|
|
88
|
-
},
|
|
89
|
-
readCacheSizeAgain: function(next) {
|
|
90
|
-
memored.size(function(err, size) {
|
|
91
|
-
console.log('Current size is 0?', size === 0);
|
|
92
|
-
next();
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}, process.exit);
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
## API
|
|
100
|
-
Documentation for every module function:
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
### setup(options)
|
|
104
|
-
|
|
105
|
-
This function is used to configure _memored_.
|
|
106
|
-
|
|
107
|
-
**Arguments**:
|
|
108
|
-
|
|
109
|
-
- **purgeInterval** {Number} (optional): Configures and triggers _memored_ expired entries auto deletion. Value expressed in milliseconds. _It's only used when called this method from the master process of your application_.
|
|
110
|
-
- **logger** {Object} (optional): In you want _memored_ to log something, you must provide an object implementing *log* and *warn* functions.
|
|
111
|
-
|
|
112
|
-
**Example**:
|
|
113
|
-
```javascript
|
|
114
|
-
memored.setup({
|
|
115
|
-
purgeInterval: 15000,
|
|
116
|
-
logger: console
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### store(key, value, [ttl], callback)
|
|
121
|
-
|
|
122
|
-
This function stores a value in the cache.
|
|
123
|
-
_It is intended to be called from a worker process_.
|
|
124
|
-
|
|
125
|
-
**Arguments**:
|
|
126
|
-
|
|
127
|
-
- **key** {String} (required): Key used to lookup the entry
|
|
128
|
-
- **value** {Mixed} (required): Whatever you want to store
|
|
129
|
-
- **ttl** {Number} (optional): Time to live for this value in the cache (milliseconds)
|
|
130
|
-
- **callback** {Function} (optional): Function to be call on store completion. Callback arguments:
|
|
131
|
-
- _err_ {Error}: Optional error
|
|
132
|
-
- _expirationTime_ {Number}: The timestamp of the moment when this entry will expire. If _ttl_ is not used, this value will be undefined.
|
|
133
|
-
|
|
134
|
-
**Examples**:
|
|
135
|
-
```javascript
|
|
136
|
-
memored.store('key1', {firstname: 'Han', lastname: 'Solo'}, function() {
|
|
137
|
-
console.log('Value stored!');
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
memored.store('key2', ['a', 'b', 'c'], 15000, function(err, expirationTime) {
|
|
141
|
-
console.log('This value will expire on:', new Date(expirationTime));
|
|
142
|
-
});
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### read(key, callback)
|
|
146
|
-
|
|
147
|
-
This function reads a value from the cache.
|
|
148
|
-
_It is intended to be called from a worker process_.
|
|
149
|
-
|
|
150
|
-
**Arguments**:
|
|
151
|
-
|
|
152
|
-
- **key** {String} (required): Key used to lookup the entry
|
|
153
|
-
- **callback** {Function} (required): Function to be called on read completion. Callback arguments:
|
|
154
|
-
- _err_ {Error}: Optional error
|
|
155
|
-
- _value_ {Mixed}: Contents of the cached entry. If the value is not found or is expired, it will be undefined.
|
|
156
|
-
|
|
157
|
-
**Example**:
|
|
158
|
-
```javascript
|
|
159
|
-
memored.read('key1', function(err, value) {
|
|
160
|
-
console.log('Key1 value:', value);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
memored.read('key1', function(err, value, expirationTime) {
|
|
164
|
-
console.log('Key1 value:', value);
|
|
165
|
-
console.log('Key1 expiration time:', new Date(expirationTime));
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
memored.read('unknownKey', function(err, value) {
|
|
169
|
-
console.log('No data read?', value === undefined);
|
|
170
|
-
});
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### remove(key, callback)
|
|
174
|
-
|
|
175
|
-
This function removes an entry from the cache.
|
|
176
|
-
_It is intended to be called from a worker process_.
|
|
177
|
-
|
|
178
|
-
**Arguments**:
|
|
179
|
-
|
|
180
|
-
- **key** {String} (required): Key used to lookup the entry
|
|
181
|
-
- **callback** {Function} (optional): Function to be called on read completion.
|
|
182
|
-
|
|
183
|
-
**Example**:
|
|
184
|
-
```javascript
|
|
185
|
-
memored.remove('key1', function() {
|
|
186
|
-
console.log('Key removed from the cache.');
|
|
187
|
-
});
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
### clean(callback)
|
|
191
|
-
|
|
192
|
-
This function removes all the entries from the cache.
|
|
193
|
-
_It is intended to be called from a worker process_.
|
|
194
|
-
|
|
195
|
-
**Arguments**:
|
|
196
|
-
|
|
197
|
-
- **callback** {Function} (optional): Function to be called on read completion.
|
|
198
|
-
|
|
199
|
-
**Example**:
|
|
200
|
-
```javascript
|
|
201
|
-
memored.clean(function() {
|
|
202
|
-
console.log('All cache entries have been deleted.');
|
|
203
|
-
});
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
### size(callback)
|
|
207
|
-
|
|
208
|
-
This function returns the number of entries in the cache.
|
|
209
|
-
|
|
210
|
-
**Arguments**:
|
|
211
|
-
|
|
212
|
-
- **callback** {Function} (required): Function to be called on size calculation is complete. Callback arguments:
|
|
213
|
-
- _err_ {Error}: Optional error
|
|
214
|
-
- _size_ {Number}: The number of entries in the cache.
|
|
215
|
-
|
|
216
|
-
**Example**:
|
|
217
|
-
|
|
218
|
-
```javascript
|
|
219
|
-
memored.size(function(err, size) {
|
|
220
|
-
console.log('Cache size:', size);
|
|
221
|
-
});
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
### version
|
|
225
|
-
|
|
226
|
-
This is an attribute wich provides module's version number
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
# Final note
|
|
230
|
-
All the callbacks first parameter is an optional error object. Actually, this param will never be an error because there is no expected error in the internal code. There's no function call that can possible throw an expected error that this module would deal with.
|
|
231
|
-
The existence of this param is to follow the *convention* about libraries callbacks in nodejs. As everybody expects this first callback parameter to be an optional one, I decided to include it.
|
|
232
|
-
|
|
233
|
-
- [Nodeguide](http://nodeguide.com/style.html#callbacks)
|
|
234
|
-
- [Nodejitsu](http://docs.nodejitsu.com/articles/errors/what-are-the-error-conventions)
|
|
235
|
-
- [GoSquared](https://engineering.gosquared.com/node-js-error-handling-callbacks-vs-promises)
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
## License
|
|
239
|
-
Copyright (c) 2014 PaquitoSoft
|
|
240
|
-
Licensed under the MIT license.
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
var cluster = require('cluster'),
|
|
2
|
-
memored = require('../index');
|
|
3
|
-
|
|
4
|
-
if (cluster.isMaster) {
|
|
5
|
-
cluster.fork();
|
|
6
|
-
} else {
|
|
7
|
-
var han = {
|
|
8
|
-
firstname: 'Han',
|
|
9
|
-
lastname: 'Solo'
|
|
10
|
-
},
|
|
11
|
-
luke = {
|
|
12
|
-
firstname: 'Luke',
|
|
13
|
-
lastname: 'Skywalker'
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// Store and read
|
|
17
|
-
memored.store('character1', han, function() {
|
|
18
|
-
console.log('Value stored!');
|
|
19
|
-
|
|
20
|
-
memored.read('character1', function(err, value) {
|
|
21
|
-
console.log('Read value:', value);
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// You can also set a ttl (milliseconds)
|
|
26
|
-
memored.store('character2', luke, 1000, function(err, expirationTime) {
|
|
27
|
-
console.log('Value stored until:', new Date(expirationTime));
|
|
28
|
-
|
|
29
|
-
setTimeout(function() {
|
|
30
|
-
memored.read('character2', function(err, value) {
|
|
31
|
-
console.log('Value is gone?', value === undefined);
|
|
32
|
-
|
|
33
|
-
process.exit();
|
|
34
|
-
});
|
|
35
|
-
}, 1050);
|
|
36
|
-
});
|
|
37
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
var cluster = require('cluster'),
|
|
2
|
-
async = require('async'),
|
|
3
|
-
memored = require('../index');
|
|
4
|
-
|
|
5
|
-
if (cluster.isMaster) {
|
|
6
|
-
|
|
7
|
-
cluster.fork();
|
|
8
|
-
memored.setup({ purgeInterval: 500});
|
|
9
|
-
|
|
10
|
-
} else {
|
|
11
|
-
|
|
12
|
-
async.series({
|
|
13
|
-
storeValue: function(next) {
|
|
14
|
-
memored.store('key1', 'My simple string value', 100, next);
|
|
15
|
-
},
|
|
16
|
-
readCacheSize: function(next) {
|
|
17
|
-
memored.size(function(err, size) {
|
|
18
|
-
console.log('Current size is 1?', size === 1);
|
|
19
|
-
next();
|
|
20
|
-
});
|
|
21
|
-
},
|
|
22
|
-
wait: function(next) {
|
|
23
|
-
setTimeout(next, 600);
|
|
24
|
-
},
|
|
25
|
-
readCacheSizeAgain: function(next) {
|
|
26
|
-
memored.size(function(err, size) {
|
|
27
|
-
console.log('Current size is 0?', size === 0);
|
|
28
|
-
next();
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}, process.exit);
|
|
32
|
-
}
|
package/temp/memored/gulpfile.js
DELETED
package/temp/memored/index.js
DELETED
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var cluster = require('cluster'),
|
|
4
|
-
packageInfo = require('./package');
|
|
5
|
-
|
|
6
|
-
var logger = {
|
|
7
|
-
log: function() {},
|
|
8
|
-
warn: function() {}
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
var messagesCounter = 0;
|
|
12
|
-
|
|
13
|
-
var activeMessages = {};
|
|
14
|
-
|
|
15
|
-
var purgeIntervalObj;
|
|
16
|
-
|
|
17
|
-
/*
|
|
18
|
-
message
|
|
19
|
-
- workerPid
|
|
20
|
-
- type
|
|
21
|
-
- requestParams
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
var cache = {};
|
|
25
|
-
|
|
26
|
-
function CacheEntry(data) { // ttl -> milliseconds
|
|
27
|
-
this.key = data.key;
|
|
28
|
-
this.value = data.value;
|
|
29
|
-
this.creationTime = Date.now();
|
|
30
|
-
if (data.ttl) {
|
|
31
|
-
this.ttl = data.ttl;
|
|
32
|
-
this.expirationTime = this.creationTime + data.ttl;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
CacheEntry.prototype.isExpired = function() {
|
|
36
|
-
return this.expirationTime && Date.now() > this.expirationTime;
|
|
37
|
-
};
|
|
38
|
-
CacheEntry.prototype.toString = function() {
|
|
39
|
-
return "Key: " + this.key + "; Value: " + this.value + "; Ttl: " + this.ttl;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
function _findWorkerByPid(workerPid) {
|
|
43
|
-
var i = 0,
|
|
44
|
-
workerIds = Object.keys(cluster.workers),
|
|
45
|
-
len = workerIds.length,
|
|
46
|
-
worker;
|
|
47
|
-
|
|
48
|
-
for (; i < len; i++) {
|
|
49
|
-
if (cluster.workers[workerIds[i]].process.pid == workerPid) {
|
|
50
|
-
worker = cluster.workers[workerIds[i]];
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return worker;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function _getResultParamsValues(paramsObj) {
|
|
59
|
-
var result = [null],
|
|
60
|
-
prop;
|
|
61
|
-
if (paramsObj) {
|
|
62
|
-
for (prop in paramsObj) {
|
|
63
|
-
result.push(paramsObj[prop]);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function _sendMessageToWorker(message) {
|
|
70
|
-
var worker = _findWorkerByPid(message.workerPid);
|
|
71
|
-
worker.send(message);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function _sendMessageToMaster(message) {
|
|
75
|
-
message.channel = 'memored';
|
|
76
|
-
message.workerPid = process.pid;
|
|
77
|
-
message.id = process.pid + '::' + messagesCounter++;
|
|
78
|
-
process.send(message);
|
|
79
|
-
if (message.callback) {
|
|
80
|
-
activeMessages[message.id] = message;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function _readCacheValue(message) {
|
|
85
|
-
var cacheEntry = cache[message.requestParams.key];
|
|
86
|
-
if (!cacheEntry) return _sendMessageToWorker(message);
|
|
87
|
-
if (cacheEntry.isExpired()) {
|
|
88
|
-
process.nextTick(function() {
|
|
89
|
-
delete cache[message.requestParams.key];
|
|
90
|
-
});
|
|
91
|
-
cacheEntry = null;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (cacheEntry) {
|
|
95
|
-
message.responseParams = {
|
|
96
|
-
value: cacheEntry.value
|
|
97
|
-
};
|
|
98
|
-
if (cacheEntry.expirationTime) {
|
|
99
|
-
message.responseParams.expirationTime = cacheEntry.expirationTime;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
_sendMessageToWorker(message);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function _storeCacheValue(message) {
|
|
107
|
-
cache[message.requestParams.key] = new CacheEntry(message.requestParams);
|
|
108
|
-
if (message.requestParams.ttl) {
|
|
109
|
-
message.responseParams = {
|
|
110
|
-
expirationTime: cache[message.requestParams.key].expirationTime
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
_sendMessageToWorker(message);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function _removeCacheValue(message) {
|
|
117
|
-
delete cache[message.requestParams.key];
|
|
118
|
-
_sendMessageToWorker(message);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function _cleanCache(message) {
|
|
122
|
-
cache = {};
|
|
123
|
-
_sendMessageToWorker(message);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function _getCacheSize(message) {
|
|
127
|
-
message.responseParams = {
|
|
128
|
-
size: Object.keys(cache).length
|
|
129
|
-
};
|
|
130
|
-
_sendMessageToWorker(message);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function _getCacheKeys(message) {
|
|
134
|
-
message.responseParams = {
|
|
135
|
-
keys: Object.keys(cache)
|
|
136
|
-
};
|
|
137
|
-
_sendMessageToWorker(message);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function _purgeCache() {
|
|
141
|
-
var now = Date.now();
|
|
142
|
-
Object.keys(cache).forEach(function(cacheKey) {
|
|
143
|
-
if (cache[cacheKey].expirationTime && cache[cacheKey].expirationTime < now) {
|
|
144
|
-
delete cache[cacheKey];
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function _masterIncomingMessagesHanlder(message) {
|
|
150
|
-
logger.log('Master received message:', message);
|
|
151
|
-
|
|
152
|
-
if (!message || message.channel !== 'memored') return false;
|
|
153
|
-
|
|
154
|
-
switch (message.type) {
|
|
155
|
-
case 'read':
|
|
156
|
-
_readCacheValue(message);
|
|
157
|
-
break;
|
|
158
|
-
case 'store':
|
|
159
|
-
_storeCacheValue(message);
|
|
160
|
-
break;
|
|
161
|
-
case 'remove':
|
|
162
|
-
_removeCacheValue(message);
|
|
163
|
-
break;
|
|
164
|
-
case 'clean':
|
|
165
|
-
_cleanCache(message);
|
|
166
|
-
break;
|
|
167
|
-
case 'size':
|
|
168
|
-
_getCacheSize(message);
|
|
169
|
-
break;
|
|
170
|
-
case 'keys':
|
|
171
|
-
_getCacheKeys(message);
|
|
172
|
-
break;
|
|
173
|
-
default:
|
|
174
|
-
logger.warn('Received an invalid message type:', message.type);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function _workerIncomingMessagesHandler(message) {
|
|
179
|
-
logger.log('Worker received message:', message);
|
|
180
|
-
|
|
181
|
-
var pendingMessage;
|
|
182
|
-
|
|
183
|
-
if (!message || message.channel !== 'memored') return false;
|
|
184
|
-
|
|
185
|
-
pendingMessage = activeMessages[message.id];
|
|
186
|
-
if (pendingMessage && pendingMessage.callback) {
|
|
187
|
-
pendingMessage.callback.apply(null, _getResultParamsValues(message.responseParams));
|
|
188
|
-
delete activeMessages[message.id];
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (cluster.isMaster) {
|
|
194
|
-
|
|
195
|
-
Object.keys(cluster.workers).forEach(function(workerId) {
|
|
196
|
-
cluster.workers[workerId].on('message', _masterIncomingMessagesHanlder);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
// Listen for new workers so we can listen to its messages
|
|
200
|
-
cluster.on('fork', function(worker) {
|
|
201
|
-
worker.on('message', _masterIncomingMessagesHanlder);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// TODO: Only for testing purposes
|
|
205
|
-
// setInterval(function() {
|
|
206
|
-
// logger.log('\n------------------------------------------');
|
|
207
|
-
// logger.log(cache);
|
|
208
|
-
// logger.log('------------------------------------------\n');
|
|
209
|
-
// }, 2000).unref();
|
|
210
|
-
|
|
211
|
-
} else {
|
|
212
|
-
|
|
213
|
-
process.on('message', _workerIncomingMessagesHandler);
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function _setup(options) {
|
|
218
|
-
options = options || {};
|
|
219
|
-
logger = options.logger || logger;
|
|
220
|
-
|
|
221
|
-
if (cluster.isMaster) {
|
|
222
|
-
|
|
223
|
-
if (options.mockData) {
|
|
224
|
-
options.mockData.forEach(function(mock) {
|
|
225
|
-
// key, value, ttl
|
|
226
|
-
cache[mock.key] = new CacheEntry(mock);
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (options.purgeInterval) {
|
|
231
|
-
purgeIntervalObj = setInterval(function() {
|
|
232
|
-
_purgeCache();
|
|
233
|
-
}, options.purgeInterval).unref();
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function _read(key, callback) {
|
|
239
|
-
if (cluster.isWorker) {
|
|
240
|
-
_sendMessageToMaster({
|
|
241
|
-
type: 'read',
|
|
242
|
-
requestParams: {
|
|
243
|
-
key: key
|
|
244
|
-
},
|
|
245
|
-
callback: callback
|
|
246
|
-
});
|
|
247
|
-
} else {
|
|
248
|
-
logger.warn('Memored::read# Cannot call this function from master process');
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function _store(key, value, ttl, callback) {
|
|
253
|
-
if (cluster.isWorker) {
|
|
254
|
-
if (callback === undefined) {
|
|
255
|
-
callback = ttl;
|
|
256
|
-
ttl = undefined;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
_sendMessageToMaster({
|
|
260
|
-
type: 'store',
|
|
261
|
-
requestParams: {
|
|
262
|
-
key: key,
|
|
263
|
-
value: value,
|
|
264
|
-
ttl: ttl
|
|
265
|
-
},
|
|
266
|
-
callback: callback
|
|
267
|
-
});
|
|
268
|
-
} else {
|
|
269
|
-
logger.warn('Memored::store# Cannot call this function from master process');
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function _remove(key, callback) {
|
|
274
|
-
if (cluster.isWorker) {
|
|
275
|
-
_sendMessageToMaster({
|
|
276
|
-
type: 'remove',
|
|
277
|
-
requestParams: {
|
|
278
|
-
key: key
|
|
279
|
-
},
|
|
280
|
-
callback: callback
|
|
281
|
-
});
|
|
282
|
-
} else {
|
|
283
|
-
logger.warn('Memored::remove# Cannot call this function from master process');
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function _clean(callback) {
|
|
288
|
-
if (cluster.isWorker) {
|
|
289
|
-
_sendMessageToMaster({
|
|
290
|
-
type: 'clean',
|
|
291
|
-
callback: callback
|
|
292
|
-
});
|
|
293
|
-
} else {
|
|
294
|
-
logger.warn('Memored::clean# Cannot call this function from master process');
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
function _size(callback) {
|
|
299
|
-
if (cluster.isWorker) {
|
|
300
|
-
_sendMessageToMaster({
|
|
301
|
-
type: 'size',
|
|
302
|
-
callback: callback
|
|
303
|
-
});
|
|
304
|
-
} else {
|
|
305
|
-
setImmediate(callback,{
|
|
306
|
-
size: Object.keys(cache).length
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
function _reset(callback) {
|
|
312
|
-
if (cluster.isMaster) {
|
|
313
|
-
clearInterval(purgeIntervalObj);
|
|
314
|
-
setImmediate(callback);
|
|
315
|
-
} else {
|
|
316
|
-
logger.warn('Memored::reset# Cannot call this function from a worker process');
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
function _keys(callback) {
|
|
321
|
-
if (cluster.isWorker) {
|
|
322
|
-
_sendMessageToMaster({
|
|
323
|
-
type: 'keys',
|
|
324
|
-
callback: callback
|
|
325
|
-
});
|
|
326
|
-
} else {
|
|
327
|
-
setImmediate(callback,{
|
|
328
|
-
keys: Object.keys(cache)
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
module.exports = {
|
|
334
|
-
version: packageInfo.version,
|
|
335
|
-
setup: _setup,
|
|
336
|
-
read: _read,
|
|
337
|
-
store: _store,
|
|
338
|
-
remove: _remove,
|
|
339
|
-
clean: _clean,
|
|
340
|
-
size: _size,
|
|
341
|
-
reset: _reset,
|
|
342
|
-
keys: _keys
|
|
343
|
-
};
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "memored",
|
|
3
|
-
"version": "0.0.4",
|
|
4
|
-
"description": "Shared in-memory module for cluster applications",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "gulp"
|
|
8
|
-
},
|
|
9
|
-
"author": {
|
|
10
|
-
"name": "Paquitosoft"
|
|
11
|
-
},
|
|
12
|
-
"license": "MIT",
|
|
13
|
-
"keywords": [
|
|
14
|
-
"cache",
|
|
15
|
-
"memory",
|
|
16
|
-
"shared"
|
|
17
|
-
],
|
|
18
|
-
"repository": {
|
|
19
|
-
"type": "git",
|
|
20
|
-
"url": "https://github.com/PaquitoSoft/memored.git"
|
|
21
|
-
},
|
|
22
|
-
"bugs": {
|
|
23
|
-
"url": "https://github.com/PaquitoSoft/memored/issues"
|
|
24
|
-
},
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"async": "^0.9.0",
|
|
27
|
-
"chai": "^1.9.1",
|
|
28
|
-
"faker": "^1.1.0",
|
|
29
|
-
"gulp": "^3.8.7",
|
|
30
|
-
"gulp-mocha": "^1.0.0"
|
|
31
|
-
},
|
|
32
|
-
"gitHead": "37d1ab5e06ab4fc5d3ff99d36bf4be7cbe019567",
|
|
33
|
-
"homepage": "https://github.com/PaquitoSoft/memored",
|
|
34
|
-
"_id": "memored@0.0.4",
|
|
35
|
-
"_shasum": "70b21f1707cb28f04ea2ef9629109b75f8cf7791",
|
|
36
|
-
"_from": "memored@*",
|
|
37
|
-
"_npmVersion": "1.4.21",
|
|
38
|
-
"_npmUser": {
|
|
39
|
-
"name": "paquitosoft",
|
|
40
|
-
"email": "paquitosoftware@gmail.com"
|
|
41
|
-
},
|
|
42
|
-
"maintainers": [
|
|
43
|
-
{
|
|
44
|
-
"name": "paquitosoft",
|
|
45
|
-
"email": "paquitosoftware@gmail.com"
|
|
46
|
-
}
|
|
47
|
-
],
|
|
48
|
-
"dist": {
|
|
49
|
-
"shasum": "70b21f1707cb28f04ea2ef9629109b75f8cf7791",
|
|
50
|
-
"tarball": "http://registry.npmjs.org/memored/-/memored-0.0.4.tgz"
|
|
51
|
-
},
|
|
52
|
-
"directories": {},
|
|
53
|
-
"_resolved": "https://registry.npmjs.org/memored/-/memored-0.0.4.tgz"
|
|
54
|
-
}
|
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/* global describe, after, it */
|
|
4
|
-
var cluster = require('cluster'),
|
|
5
|
-
expect = require('chai').expect,
|
|
6
|
-
faker = require('faker'),
|
|
7
|
-
async = require('async'),
|
|
8
|
-
memored = require('../index.js');
|
|
9
|
-
|
|
10
|
-
function _createUser() {
|
|
11
|
-
return {
|
|
12
|
-
firstName: faker.Name.findName(),
|
|
13
|
-
lastName: faker.Name.lastName(),
|
|
14
|
-
email: faker.Internet.email(),
|
|
15
|
-
address: {
|
|
16
|
-
streetAddress: faker.Address.streetName() + ' - ' + faker.Address.streetAddress(),
|
|
17
|
-
zipCode: faker.Address.zipCode(),
|
|
18
|
-
city: faker.Address.city()
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
describe('Memored test suite', function() {
|
|
24
|
-
|
|
25
|
-
if (cluster.isMaster) {
|
|
26
|
-
cluster.fork();
|
|
27
|
-
|
|
28
|
-
describe('Memored - purge', function() {
|
|
29
|
-
|
|
30
|
-
var mockedData = [
|
|
31
|
-
{
|
|
32
|
-
key: 'mock1',
|
|
33
|
-
value: _createUser(),
|
|
34
|
-
ttl: 25
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
key: 'mock2',
|
|
38
|
-
value: _createUser(),
|
|
39
|
-
ttl: 75
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
key: 'mock3',
|
|
43
|
-
value: _createUser()
|
|
44
|
-
}
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
it.only('Should auto-remove old data if configured to purge', function(done) {
|
|
48
|
-
async.series({
|
|
49
|
-
setup: function(next) {
|
|
50
|
-
memored.setup({purgeInterval: 50, mockData: mockedData});
|
|
51
|
-
next();
|
|
52
|
-
},
|
|
53
|
-
getCacheSize: function(next) {
|
|
54
|
-
memored.size(function(data) {
|
|
55
|
-
expect(data.size).to.equal(3);
|
|
56
|
-
next();
|
|
57
|
-
});
|
|
58
|
-
},
|
|
59
|
-
wait: function(next) {
|
|
60
|
-
setTimeout(next, 60);
|
|
61
|
-
},
|
|
62
|
-
getCacheSize2: function(next) {
|
|
63
|
-
memored.size(function(data) {
|
|
64
|
-
expect(data.size).to.equal(2);
|
|
65
|
-
next();
|
|
66
|
-
});
|
|
67
|
-
},
|
|
68
|
-
waitAgain: function(next) {
|
|
69
|
-
setTimeout(next, 50);
|
|
70
|
-
},
|
|
71
|
-
getCacheSize3: function(next) {
|
|
72
|
-
memored.size(function(data) {
|
|
73
|
-
expect(data.size).to.equal(1);
|
|
74
|
-
next();
|
|
75
|
-
});
|
|
76
|
-
},
|
|
77
|
-
}, done);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
} else {
|
|
82
|
-
|
|
83
|
-
after(function() {
|
|
84
|
-
process.exit();
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe('Memored - store', function() {
|
|
88
|
-
|
|
89
|
-
it('Should store a value in the cache', function(done) {
|
|
90
|
-
var user1 = _createUser();
|
|
91
|
-
memored.store('user1', user1, function(err, expirationTime) {
|
|
92
|
-
expect(err).to.equal(null);
|
|
93
|
-
expect(expirationTime).to.equal(undefined);
|
|
94
|
-
done();
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('Should store a value and create an expiration time when ttl is used', function(done) {
|
|
99
|
-
var user2 = _createUser(),
|
|
100
|
-
t1 = Date.now();
|
|
101
|
-
memored.store('user2', user2, 100, function(err, expirationTime) {
|
|
102
|
-
expect(err).to.equals(null);
|
|
103
|
-
expect(expirationTime).to.be.a('number');
|
|
104
|
-
expect(expirationTime).to.be.least(t1 + 100);
|
|
105
|
-
done();
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('Memored - read', function() {
|
|
112
|
-
|
|
113
|
-
it('Should read a cache entry', function(done) {
|
|
114
|
-
var user3 = _createUser();
|
|
115
|
-
async.series({
|
|
116
|
-
storeValue: function(next) {
|
|
117
|
-
memored.store('user3', user3, next);
|
|
118
|
-
},
|
|
119
|
-
readValue: function(next) {
|
|
120
|
-
memored.read('user3', function(err, value) {
|
|
121
|
-
expect(err).to.equals(null);
|
|
122
|
-
expect(value).to.eql(user3);
|
|
123
|
-
next();
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
}, done);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('Should return an undefined entry when looking for a non-existing cache entry', function(done) {
|
|
130
|
-
memored.read('unknownKey', function(err, value) {
|
|
131
|
-
expect(err).to.equals(null);
|
|
132
|
-
expect(value).to.equal(undefined);
|
|
133
|
-
done();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('Should respect cache entry ttl', function(done) {
|
|
138
|
-
var user4 = _createUser(),
|
|
139
|
-
t1 = Date.now();
|
|
140
|
-
|
|
141
|
-
async.series({
|
|
142
|
-
storeValue: function(next) {
|
|
143
|
-
memored.store('user4', user4, 20, next);
|
|
144
|
-
},
|
|
145
|
-
readValue1: function(next) {
|
|
146
|
-
memored.read('user4', function(err, value, expirationTime) {
|
|
147
|
-
expect(err).to.equals(null);
|
|
148
|
-
expect(value).to.eql(user4);
|
|
149
|
-
expect(expirationTime).to.least(t1 + 20);
|
|
150
|
-
next();
|
|
151
|
-
});
|
|
152
|
-
},
|
|
153
|
-
readValue2: function(next) {
|
|
154
|
-
setTimeout(function() {
|
|
155
|
-
memored.read('user4', function(err, value) {
|
|
156
|
-
expect(err).to.equals(null);
|
|
157
|
-
expect(value).to.equal(undefined);
|
|
158
|
-
next();
|
|
159
|
-
});
|
|
160
|
-
}, 30);
|
|
161
|
-
}
|
|
162
|
-
}, done);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
describe('Memored - remove', function() {
|
|
168
|
-
|
|
169
|
-
it('Should remove a cache entry', function(done) {
|
|
170
|
-
var user5 = _createUser();
|
|
171
|
-
async.series({
|
|
172
|
-
storeValue: function(next) {
|
|
173
|
-
memored.store('user5', user5, next);
|
|
174
|
-
},
|
|
175
|
-
readValue1: function(next) {
|
|
176
|
-
memored.read('user5', function(err, value) {
|
|
177
|
-
expect(err).to.equals(null);
|
|
178
|
-
expect(value).to.eql(user5);
|
|
179
|
-
next();
|
|
180
|
-
});
|
|
181
|
-
},
|
|
182
|
-
removeValue: function(next) {
|
|
183
|
-
memored.remove('user5', next);
|
|
184
|
-
},
|
|
185
|
-
readValue2: function(next) {
|
|
186
|
-
memored.read('user5', function(err, value) {
|
|
187
|
-
expect(err).to.equals(null);
|
|
188
|
-
expect(value).to.equal(undefined);
|
|
189
|
-
next();
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
}, done);
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
describe('Memored - clean', function() {
|
|
198
|
-
it('Should remove all values in cache', function(done) {
|
|
199
|
-
var user6 = _createUser(),
|
|
200
|
-
user7 = _createUser();
|
|
201
|
-
|
|
202
|
-
async.series({
|
|
203
|
-
soreValue1: function(next) {
|
|
204
|
-
memored.store('user6', user6, next);
|
|
205
|
-
},
|
|
206
|
-
storeValue2: function(next) {
|
|
207
|
-
memored.store('user7', user7, next);
|
|
208
|
-
},
|
|
209
|
-
readValue1: function(next) {
|
|
210
|
-
memored.read('user6', function(err, value) {
|
|
211
|
-
expect(err).to.equals(null);
|
|
212
|
-
expect(value).to.eql(user6);
|
|
213
|
-
next();
|
|
214
|
-
});
|
|
215
|
-
},
|
|
216
|
-
readValue2: function(next) {
|
|
217
|
-
memored.read('user7', function(err, value) {
|
|
218
|
-
expect(err).to.equals(null);
|
|
219
|
-
expect(value).to.eql(user7);
|
|
220
|
-
next();
|
|
221
|
-
});
|
|
222
|
-
},
|
|
223
|
-
cleanCache: function(next) {
|
|
224
|
-
memored.clean(next);
|
|
225
|
-
},
|
|
226
|
-
readValue4: function(next) {
|
|
227
|
-
memored.read('user6', function(err, value) {
|
|
228
|
-
expect(err).to.equals(null);
|
|
229
|
-
expect(value).to.equal(undefined);
|
|
230
|
-
next();
|
|
231
|
-
});
|
|
232
|
-
},
|
|
233
|
-
readValue5: function(next) {
|
|
234
|
-
memored.read('user7', function(err, value) {
|
|
235
|
-
expect(err).to.equals(null);
|
|
236
|
-
expect(value).to.equal(undefined);
|
|
237
|
-
next();
|
|
238
|
-
});
|
|
239
|
-
},
|
|
240
|
-
}, done);
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
describe('Memored - setup -- logger', function() {
|
|
245
|
-
var customLogger = {
|
|
246
|
-
messages: [],
|
|
247
|
-
log: function() {
|
|
248
|
-
this.messages.push(Array.prototype.slice.call(arguments).join(','));
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
it('Should use a custom logger when requested', function(done) {
|
|
252
|
-
memored.setup({
|
|
253
|
-
logger: customLogger
|
|
254
|
-
});
|
|
255
|
-
expect(customLogger.messages).to.be.have.length(0);
|
|
256
|
-
memored.store('user8', _createUser(), function() {
|
|
257
|
-
expect(customLogger.messages).to.have.length(1);
|
|
258
|
-
done();
|
|
259
|
-
});
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
});
|