This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cached-request.js
169 lines (148 loc) · 4.16 KB
/
cached-request.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
var log4js = require('log4js'),
moment = require('moment'),
Q = require('q'),
request = require('request'),
instances = 0;
/**
* @class CachedRequest
* @param params
* @constructor
* @example
* new CachedRequest({
* cacheThreshold: 10, //in minutes, default: 1
* auth: { //optional
* user: "foo",
* pass: "bar"
* },
* logging: true //default: false,
* userAgent: 'My custom user agent' //optional
* });
*/
var CachedRequest = function(params) {
var config = Object.assign({}, {
cacheThreshold: 1,
auth: null,
logging: false,
userAgent: 'CachedRequest - https://github.com/dios-david/cached-request'
}, params);
this.cache = {};
this.cacheThreshold = config.cacheThreshold;
this.auth = config.auth;
this.userAgent = config.userAgent;
this.logging = config.logging;
this.logger = log4js.getLogger('CachedRequest #' + instances++);
if(!this.logging) {
this.logger.setLevel(log4js.levels.OFF);
}
};
CachedRequest.prototype = {
/**
* @property cacheThreshold
* @type number
*/
cacheThreshold: 0,
/**
* @property cache
* @type object
*/
cache: {},
/**
* @method get
* @param url
*/
get: function(url) {
var deferred = Q.defer(),
cachedData = this.cache[url];
if(cachedData) {
if (moment(cachedData.time).isAfter(moment().subtract(this.cacheThreshold, 'minutes'))) {
this.logger.info(url, 'has a valid cache');
deferred.resolve(cachedData.data);
return deferred.promise;
} else {
this.logger.info(url, 'has a cache but its expired');
delete this.cache[url];
}
}
this.logger.info(url, 'has no cache');
return this.request(url, 'GET');
},
/**
* @method post
* @param url
* @param postData
*/
post: function(url, postData) {
var deferred = Q.defer(),
cachedData = this.cache[url + JSON.stringify(postData)];
if(cachedData) {
if (moment(cachedData.time).isAfter(moment().subtract(this.cacheThreshold, 'minutes'))) {
this.logger.info(url, 'has a valid cache');
deferred.resolve(cachedData.data);
return deferred.promise;
} else {
this.logger.info(url, 'has a cache but its expired');
delete this.cache[url];
}
}
this.logger.info(url, 'has no cache');
return this.request(url, 'POST', postData);
},
/**
* @method request
* @param url
* @param method
* @param postData
*/
request: function(url, method, postData) {
var deferred = Q.defer(),
self = this;
this.logger.info(url, 'request started');
request({
url: url,
method: method,
form: postData,
auth: this.auth,
rejectUnauthorized: false,
headers: {
'User-Agent': this.userAgent
}
}, function (error, response, body) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(body);
}
});
Q.when(deferred.promise,
function (data) {
self.logger.info(url, 'request success');
self.storeCache(postData ? url + JSON.stringify(postData) : url, data);
},
function (error) {
self.logger.warn(url, 'request error');
}
);
return deferred.promise;
},
/**
* @method storeCache
* @param url
* @param data
*/
storeCache: function(url, data) {
this.cache[url] = new CachedRequestObject(url, data, new Date());
}
};
/**
* @class CachedRequestObject
* @param url
* @param data
* @param time
* @constructor
*/
CachedRequestObject = function(url, data, time) {
this.url = url;
this.data = data;
this.time = time;
};
module.exports = CachedRequest;