-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BotCache.ts
56 lines (48 loc) · 1.53 KB
/
BotCache.ts
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
type BotCacheKey = {
STATUSES_ASKED_TODAY: string;
USERS_WITHOUT_STATUS: string;
STATUSES_COLLECTED: string;
DAILY_STATUSES: string;
statusDropTime: (user: string) => string;
dailyStatus: (user: string) => string;
}
class BotCache {
static cache: any = CacheService.getScriptCache();
static MAX_EXPIRATION: number = 21600; // 6 hours;
static Key: BotCacheKey = {
STATUSES_COLLECTED: 'STATISTICS_UPDATED_TODAY',
STATUSES_ASKED_TODAY: 'STATUSES_ASKED_TODAY',
DAILY_STATUSES: 'DAILY_STATUSES',
USERS_WITHOUT_STATUS: 'USERS_WITHOUT_STATUS',
dailyStatus: user => 'DailyStatus_' + user,
statusDropTime: user => 'StatusDropTime_' + user,
};
static put(key, value, expirationInSeconds = BotCache.MAX_EXPIRATION) {
this.cache.remove(key);
if (value) {
this.cache.put(key, JSON.stringify(value), expirationInSeconds);
}
return value;
}
static get(key, defaultValue = null) {
try {
const value = BotCache.cache.get(key);
return value ? JSON.parse(value) : defaultValue;
} catch (e) {
return defaultValue;
}
}
/**
* Gets value from the cache or if absent, puts the value from valueGetter and returns it.
* @param key {string}
* @param valueGetter {function}
* @param expirationInSeconds {int}
* @returns {any}
*/
static getOrPut(key, valueGetter, expirationInSeconds = BotCache.MAX_EXPIRATION) {
return BotCache.get(key) || BotCache.put(key, valueGetter(), expirationInSeconds);
}
static remove(key) {
BotCache.cache.remove(key);
}
}