Skip to content

Commit ba59eee

Browse files
committed
[core.uiSettings] Add cache for raw settings.
1 parent dfca5d4 commit ba59eee

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/core/public/ui_settings/ui_settings_client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class UiSettingsClient implements IUiSettingsClient {
3434
constructor(params: UiSettingsClientParams) {
3535
this.api = params.api;
3636
this.defaults = cloneDeep(params.defaults);
37-
this.cache = defaultsDeep({}, this.defaults, cloneDeep(params.initialSettings));
37+
this.cache = defaultsDeep({ ...this.defaults }, cloneDeep(params.initialSettings));
3838

3939
params.done$.subscribe({
4040
complete: () => {
@@ -177,7 +177,7 @@ You can use \`IUiSettingsClient.get("${key}", defaultValue)\`, which will just r
177177

178178
try {
179179
const { settings } = await this.api.batchSet(key, newVal);
180-
this.cache = defaultsDeep({}, defaults, settings);
180+
this.cache = defaultsDeep({ ...defaults }, settings);
181181
this.saved$.next({ key, newValue: newVal, oldValue: initialVal });
182182
return true;
183183
} catch (error) {

src/core/server/ui_settings/ui_settings_client.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9+
import _ from 'lodash';
910
import Chance from 'chance';
1011
import { schema } from '@kbn/config-schema';
1112

@@ -724,18 +725,25 @@ describe('ui settings', () => {
724725
});
725726

726727
it('getAll', async () => {
728+
const lodashDefaultsDeep = jest.spyOn(_, 'defaultsDeep');
729+
727730
const esDocSource = {};
728731
const { uiSettings, savedObjectsClient } = setup({ esDocSource });
729732

730733
await uiSettings.getAll();
731734
expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
735+
expect(lodashDefaultsDeep).toHaveBeenCalledTimes(1);
732736

733737
await uiSettings.getAll();
734738
expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
739+
expect(lodashDefaultsDeep).toHaveBeenCalledTimes(1);
735740

736741
jest.advanceTimersByTime(10000);
737742
await uiSettings.getAll();
738743
expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
744+
expect(lodashDefaultsDeep).toHaveBeenCalledTimes(2);
745+
746+
lodashDefaultsDeep.mockRestore();
739747
});
740748

741749
it('getUserProvided', async () => {

src/core/server/ui_settings/ui_settings_client.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class UiSettingsClient implements IUiSettingsClient {
4949
private readonly defaults: NonNullable<UiSettingsServiceOptions['defaults']>;
5050
private readonly log: Logger;
5151
private readonly cache: Cache;
52+
private readonly rawCache: Cache;
5253

5354
constructor(options: UiSettingsServiceOptions) {
5455
const { type, id, buildNum, savedObjectsClient, log, defaults = {}, overrides = {} } = options;
@@ -60,6 +61,7 @@ export class UiSettingsClient implements IUiSettingsClient {
6061
this.overrides = overrides;
6162
this.log = log;
6263
this.cache = new Cache();
64+
this.rawCache = new Cache();
6365
}
6466

6567
getRegistered() {
@@ -107,6 +109,7 @@ export class UiSettingsClient implements IUiSettingsClient {
107109

108110
async setMany(changes: Record<string, any>) {
109111
this.cache.del();
112+
this.rawCache.del();
110113
this.onWriteHook(changes);
111114
await this.write({ changes });
112115
}
@@ -143,8 +146,17 @@ export class UiSettingsClient implements IUiSettingsClient {
143146
}
144147

145148
private async getRaw(): Promise<UiSettingsRaw> {
149+
const cachedValue = this.rawCache.get();
150+
if (cachedValue) {
151+
return cachedValue;
152+
}
153+
146154
const userProvided = await this.getUserProvided();
147-
return defaultsDeep({}, userProvided, this.defaults);
155+
const result = defaultsDeep({ ...userProvided }, this.defaults);
156+
157+
this.rawCache.set(result);
158+
159+
return result;
148160
}
149161

150162
private validateKey(key: string, value: unknown) {

0 commit comments

Comments
 (0)