-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (53 loc) · 1.59 KB
/
index.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
/**
* @module co-cacher
*/
(function () {
"use strict";
var config = require('config'),
utils = require("cacher-utils"),
cacheStorage = utils.getCacheStorage();
/**
* Cahe wrapper
*
* @param {(function|generator)} myGenerator
* @param {Array} args
* @param {number|object} options (if number options = expires)
* @return {*}
*
* @example
* var co = require('co'),
* wait = require('co-wait'),
* cacher = require('co-cacher');
*
* var testGenerator = function* testGenerator (a) {
* yield wait(1000);
* return a+1;
* };
*
* co(function *(){
* var result = yield cacher(testGenerator, [4], {expires: 30});
* console.log(result);
* }).catch(function(e) {throw e; });
*
*/
module.exports = function* (myGenerator, args, options) {
if (typeof options == 'object') {
} else {
var expires = options;
options = {};
options.expires = expires;
}
var time = options.expires || config.cache.expires;
var salt = options.salt || '';
var key = utils.keyMaker(myGenerator.name, salt, args);
var value = yield cacheStorage.get(key);
if (typeof value != 'undefined') {
return value;
}
var data = yield myGenerator.apply(null,args);
cacheStorage.set(key,data,time).catch(function (error) {
console.warn('Cacher error -->', error.toString(), myGenerator.name, args, options);
});
return data;
}
}());