File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 8
8
## Usage
9
9
10
10
You probably shouldn't, at least directly.
11
+
12
+ ## EndpointCache
13
+
14
+ - uses ` mnemonist/lru-cache ` for storing the cache.
15
+ - the ` set ` operation stores milliseconds elapsed since the UNIX epoch in Expires param based on CachePeriodInMinutes provided in Endpoint.
16
+ - the ` get ` operation returns a random un-expired endpoint.
Original file line number Diff line number Diff line change @@ -21,6 +21,12 @@ export class EndpointCache {
21
21
return endpoints [ Math . floor ( Math . random ( ) * endpoints . length ) ] ;
22
22
}
23
23
24
+ /**
25
+ * Returns an un-expired endpoint for the given key.
26
+ *
27
+ * @param key
28
+ * @returns
29
+ */
24
30
get ( key : string ) {
25
31
if ( ! this . has ( key ) ) {
26
32
return ;
@@ -38,6 +44,16 @@ export class EndpointCache {
38
44
return endpoint ;
39
45
}
40
46
47
+ /**
48
+ * Stores the endpoints passed for the key in cache.
49
+ * If not defined, uses empty string for the Address in endpoint.
50
+ * If not defined, uses one minute for CachePeriodInMinutes in endpoint.
51
+ * Stores milliseconds elapsed since the UNIX epoch in Expires param based
52
+ * on value provided in CachePeriodInMinutes.
53
+ *
54
+ * @param key
55
+ * @param endpoints
56
+ */
41
57
set ( key : string , endpoints : Endpoint [ ] ) {
42
58
const now = Date . now ( ) ;
43
59
this . cache . set (
@@ -49,16 +65,30 @@ export class EndpointCache {
49
65
) ;
50
66
}
51
67
68
+ /**
69
+ * Removes the value for the given key in the cache.
70
+ *
71
+ * @param {string } key
72
+ */
52
73
remove ( key : string ) {
53
74
// Replace with remove call once support is added upstream
54
75
// Refs: https://github.com/Yomguithereal/mnemonist/issues/143
55
76
this . cache . set ( key , [ ] ) ;
56
77
}
57
78
58
- has ( key : string ) {
79
+ /**
80
+ * Checks whether the key exists in cache.
81
+ *
82
+ * @param {string } key
83
+ * @returns {boolean }
84
+ */
85
+ has ( key : string ) : boolean {
59
86
return this . cache . has ( key ) ;
60
87
}
61
88
89
+ /**
90
+ * Clears the cache.
91
+ */
62
92
clear ( ) {
63
93
this . cache . clear ( ) ;
64
94
}
You can’t perform that action at this time.
0 commit comments