-
Notifications
You must be signed in to change notification settings - Fork 45
/
cache.ts
113 lines (99 loc) · 3.02 KB
/
cache.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
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
import moment from 'moment';
interface CacheInterface<T> {
hasKey(key: string): boolean;
get(key: string): T | undefined;
set(key: string, item: T, expirationTime?: number): void;
}
/**
* @hidden
*/
export interface CacheEntry<T> {
expires?: moment.Moment;
entry: T;
}
/**
* Enumerator that selects the isolation type of destination in cache.
*/
export enum IsolationStrategy {
Tenant = 'Tenant',
User = 'User',
Tenant_User = 'TenantUser',
No_Isolation = 'NoIsolation'
}
export interface CachingOptions {
/**
* A boolean value that indicates whether to read destinations from cache.
*/
useCache?: boolean;
/**
* The isolation strategy used for caching destinations. For the available options, see [[IsolationStrategy]].
* By default, IsolationStrategy.Tenant is set.
*/
isolationStrategy?: IsolationStrategy; // TODO: this is kind of too generic. For destinations, this makes sense, whereas for caching access tokens this has no effect at all.
}
/**
* Representation of a cache to transiently store objects locally for faster access.
* @template T
*/
export class Cache<T> implements CacheInterface<T> {
/**
* Object that stores all cached entries.
*/
private cache: Record<string, CacheEntry<T>>;
/**
* Default validity period for each entry in cache.
* If undefined, all cached entries will be valid indefinitely.
*/
private defaultValidityTime: moment.MomentInputObject | undefined;
constructor(validityTime?: moment.MomentInputObject) {
this.cache = {};
this.defaultValidityTime = validityTime;
}
/**
* Clear all cached items.
*/
clear(): void {
this.cache = {};
}
/**
* Specifies whether an entry with a given key is defined in cache.
* @param key - The entry's key
* @returns boolean A boolean value that indicates whether the entry exists in cache
*/
hasKey(key: string): boolean {
return this.cache.hasOwnProperty(key);
}
/**
* Getter of cached entries.
* @param key - The key of the entry to retrieve
* @returns The corresponding entry to the provided key if it is still valid, returns undefined otherwise
*/
get(key: string): T | undefined {
return this.hasKey(key) && !isExpired(this.cache[key])
? this.cache[key].entry
: undefined;
}
/**
* Setter of entries in cache.
* @param key - The entry's key
* @param entry - The entry to cache
* @param expirationTime - The time expressed in UTC in which the given entry expires
*/
set(key: string, entry: T, expirationTime?: number): void {
const expires = expirationTime
? moment(expirationTime)
: inferExpirationTime(this.defaultValidityTime);
this.cache[key] = { entry, expires };
}
}
function isExpired<T>(item: CacheEntry<T>): boolean {
if (item.expires === undefined) {
return false;
}
return !item.expires.isAfter(moment());
}
function inferExpirationTime(
expirationTime: moment.MomentInputObject | undefined
): moment.Moment | undefined {
return expirationTime ? moment().add(expirationTime) : undefined;
}