Skip to content

Commit 06c674a

Browse files
committed
docs(endpoint-cache): add general description
1 parent 91227a9 commit 06c674a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/endpoint-cache/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
## Usage
99

1010
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.

packages/endpoint-cache/src/EndpointCache.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export class EndpointCache {
2121
return endpoints[Math.floor(Math.random() * endpoints.length)];
2222
}
2323

24+
/**
25+
* Returns an un-expired endpoint for the given key.
26+
*
27+
* @param key
28+
* @returns
29+
*/
2430
get(key: string) {
2531
if (!this.has(key)) {
2632
return;
@@ -38,6 +44,16 @@ export class EndpointCache {
3844
return endpoint;
3945
}
4046

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+
*/
4157
set(key: string, endpoints: Endpoint[]) {
4258
const now = Date.now();
4359
this.cache.set(
@@ -49,16 +65,30 @@ export class EndpointCache {
4965
);
5066
}
5167

68+
/**
69+
* Removes the value for the given key in the cache.
70+
*
71+
* @param {string} key
72+
*/
5273
remove(key: string) {
5374
// Replace with remove call once support is added upstream
5475
// Refs: https://github.com/Yomguithereal/mnemonist/issues/143
5576
this.cache.set(key, []);
5677
}
5778

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 {
5986
return this.cache.has(key);
6087
}
6188

89+
/**
90+
* Clears the cache.
91+
*/
6292
clear() {
6393
this.cache.clear();
6494
}

0 commit comments

Comments
 (0)