Skip to content

Commit 8127d54

Browse files
committed
Add one day cache layer for indexPattern search
1 parent 5252403 commit 8127d54

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

package-lock.json

Lines changed: 43 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"npm": "^11.8"
2727
},
2828
"dependencies": {
29+
"p-memoize": "^8.0.0",
2930
"rison2": "^0.3.0"
3031
}
3132
}

src/open-distro/client.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
22

3+
import pMemoize from 'p-memoize';
4+
35
export type MonitorResponse = {
46
id: string,
57
indexId: string,
@@ -10,6 +12,12 @@ export class OpendistroClient {
1012
private readonly password: string;
1113
private readonly dashboardPrivateUrl: string;
1214

15+
// Cache TTL: 1 day in milliseconds (Step 1)
16+
private static readonly INDEX_PATTERN_CACHE_TTL = 24 * 60 * 60 * 1000;
17+
18+
// Memoized function for index pattern lookup (created per-instance, Step 3)
19+
private readonly getIndexPatternMemoized: (indexName: string) => Promise<string>;
20+
1321
constructor(
1422
dashboardPrivateUrl: string,
1523
username: string,
@@ -18,6 +26,12 @@ export class OpendistroClient {
1826
this.dashboardPrivateUrl = dashboardPrivateUrl;
1927
this.username = username;
2028
this.password = password;
29+
30+
// Create memoized version of the helper (Step 3)
31+
this.getIndexPatternMemoized = pMemoize(
32+
this._getIndexPatternIdByIndexName.bind(this),
33+
{ maxAge: OpendistroClient.INDEX_PATTERN_CACHE_TTL }
34+
);
2135
}
2236

2337

@@ -51,6 +65,11 @@ export class OpendistroClient {
5165
}
5266

5367
public async getIndexPatternIdByIndexName(indexName: string): Promise<string>
68+
{
69+
return this.getIndexPatternMemoized(indexName);
70+
}
71+
72+
private async _getIndexPatternIdByIndexName(indexName: string): Promise<string>
5473
{
5574
const base = indexName.split('-');
5675
if (base[base.length - 1] === '*') {

0 commit comments

Comments
 (0)