forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionTokenCache.js
More file actions
38 lines (34 loc) · 1.07 KB
/
SessionTokenCache.js
File metadata and controls
38 lines (34 loc) · 1.07 KB
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
import Parse from 'parse/node';
import LRU from 'lru-cache';
import PLog from './PLog';
class SessionTokenCache {
cache: Object;
constructor(timeout: number = 30 * 24 * 60 *60 * 1000, maxSize: number = 10000) {
this.cache = new LRU({
max: maxSize,
maxAge: timeout
});
}
getUserId(sessionToken: string): any {
if (!sessionToken) {
return Parse.Promise.error('Empty sessionToken');
}
let userId = this.cache.get(sessionToken);
if (userId) {
PLog.verbose('Fetch userId %s of sessionToken %s from Cache', userId, sessionToken);
return Parse.Promise.as(userId);
}
return Parse.User.become(sessionToken).then((user) => {
PLog.verbose('Fetch userId %s of sessionToken %s from Parse', user.id, sessionToken);
let userId = user.id;
this.cache.set(sessionToken, userId);
return Parse.Promise.as(userId);
}, (error) => {
PLog.error('Can not fetch userId for sessionToken %j, error %j', sessionToken, error);
return Parse.Promise.error(error);
});
}
}
export {
SessionTokenCache
}