-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/plugins/data/server/search/services/cached_ui_settings_client.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { coreMock, httpServerMock } from '../../../../../core/server/mocks'; | ||
|
||
import { CachedUiSettingsClient } from './cached_ui_settings_client'; | ||
|
||
test('fetching uiSettings once for the same key', async () => { | ||
const request = httpServerMock.createKibanaRequest(); | ||
const soClient = coreMock.createStart().savedObjects.getScopedClient(request); | ||
const uiSettings = coreMock.createStart().uiSettings.asScopedToClient(soClient); | ||
|
||
const key = 'key'; | ||
const value = 'value'; | ||
const spy = jest | ||
.spyOn(uiSettings, 'getAll') | ||
.mockImplementation(() => Promise.resolve({ [key]: value })); | ||
|
||
const cachedUiSettings = new CachedUiSettingsClient(uiSettings); | ||
|
||
const res1 = cachedUiSettings.get(key); | ||
const res2 = cachedUiSettings.get(key); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); // check that internally uiSettings.getAll() called only once | ||
|
||
expect(await res1).toBe(value); | ||
expect(await res2).toBe(value); | ||
}); | ||
|
||
test('fetching uiSettings once for different keys', async () => { | ||
const request = httpServerMock.createKibanaRequest(); | ||
const soClient = coreMock.createStart().savedObjects.getScopedClient(request); | ||
const uiSettings = coreMock.createStart().uiSettings.asScopedToClient(soClient); | ||
|
||
const key1 = 'key1'; | ||
const value1 = 'value1'; | ||
|
||
const key2 = 'key2'; | ||
const value2 = 'value2'; | ||
|
||
const spy = jest | ||
.spyOn(uiSettings, 'getAll') | ||
.mockImplementation(() => Promise.resolve({ [key1]: value1, [key2]: value2 })); | ||
|
||
const cachedUiSettings = new CachedUiSettingsClient(uiSettings); | ||
|
||
const res1 = cachedUiSettings.get(key1); | ||
const res2 = cachedUiSettings.get(key2); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); // check that internally uiSettings.getAll() called only once | ||
|
||
expect(await res1).toBe(value1); | ||
expect(await res2).toBe(value2); | ||
}); |
44 changes: 44 additions & 0 deletions
44
src/plugins/data/server/search/services/cached_ui_settings_client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { IUiSettingsClient } from 'kibana/server'; | ||
|
||
/** | ||
* {@link IUiSettingsClient} wrapper to ensure uiSettings requested only once within a single KibanaRequest, | ||
* {@link IUiSettingsClient} has its own cache, but it doesn't cache pending promises, so this produces two requests: | ||
* | ||
* const promise1 = uiSettings.get(1); // fetches config | ||
* const promise2 = uiSettings.get(2); // fetches config | ||
* | ||
* And {@link CachedUiSettingsClient} solves it, so this produced a single request: | ||
* | ||
* const promise1 = cachedUiSettingsClient.get(1); // fetches config | ||
* const promise2 = cachedUiSettingsClient.get(2); // reuses existing promise | ||
* | ||
* @internal | ||
*/ | ||
export class CachedUiSettingsClient implements Pick<IUiSettingsClient, 'get'> { | ||
private cache: Promise<Record<string, unknown>> | undefined; | ||
|
||
constructor(private readonly client: IUiSettingsClient) {} | ||
|
||
async get<T = any>(key: string): Promise<T> { | ||
if (!this.cache) { | ||
// caching getAll() instead of just get(key) because internally uiSettings calls `getAll()` anyways | ||
// this way we reuse cache in case settings for different keys were requested | ||
this.cache = this.client.getAll(); | ||
} | ||
|
||
return this.cache | ||
.then((cache) => cache[key] as T) | ||
.catch((e) => { | ||
this.cache = undefined; | ||
throw e; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { CachedUiSettingsClient } from './cached_ui_settings_client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters