Skip to content

Commit

Permalink
[core.uiSettings] Add cache for raw settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Apr 14, 2021
1 parent dfca5d4 commit ba59eee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/core/public/ui_settings/ui_settings_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class UiSettingsClient implements IUiSettingsClient {
constructor(params: UiSettingsClientParams) {
this.api = params.api;
this.defaults = cloneDeep(params.defaults);
this.cache = defaultsDeep({}, this.defaults, cloneDeep(params.initialSettings));
this.cache = defaultsDeep({ ...this.defaults }, cloneDeep(params.initialSettings));

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

try {
const { settings } = await this.api.batchSet(key, newVal);
this.cache = defaultsDeep({}, defaults, settings);
this.cache = defaultsDeep({ ...defaults }, settings);
this.saved$.next({ key, newValue: newVal, oldValue: initialVal });
return true;
} catch (error) {
Expand Down
8 changes: 8 additions & 0 deletions src/core/server/ui_settings/ui_settings_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import _ from 'lodash';
import Chance from 'chance';
import { schema } from '@kbn/config-schema';

Expand Down Expand Up @@ -724,18 +725,25 @@ describe('ui settings', () => {
});

it('getAll', async () => {
const lodashDefaultsDeep = jest.spyOn(_, 'defaultsDeep');

const esDocSource = {};
const { uiSettings, savedObjectsClient } = setup({ esDocSource });

await uiSettings.getAll();
expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(lodashDefaultsDeep).toHaveBeenCalledTimes(1);

await uiSettings.getAll();
expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(lodashDefaultsDeep).toHaveBeenCalledTimes(1);

jest.advanceTimersByTime(10000);
await uiSettings.getAll();
expect(savedObjectsClient.get).toHaveBeenCalledTimes(2);
expect(lodashDefaultsDeep).toHaveBeenCalledTimes(2);

lodashDefaultsDeep.mockRestore();
});

it('getUserProvided', async () => {
Expand Down
14 changes: 13 additions & 1 deletion src/core/server/ui_settings/ui_settings_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class UiSettingsClient implements IUiSettingsClient {
private readonly defaults: NonNullable<UiSettingsServiceOptions['defaults']>;
private readonly log: Logger;
private readonly cache: Cache;
private readonly rawCache: Cache;

constructor(options: UiSettingsServiceOptions) {
const { type, id, buildNum, savedObjectsClient, log, defaults = {}, overrides = {} } = options;
Expand All @@ -60,6 +61,7 @@ export class UiSettingsClient implements IUiSettingsClient {
this.overrides = overrides;
this.log = log;
this.cache = new Cache();
this.rawCache = new Cache();
}

getRegistered() {
Expand Down Expand Up @@ -107,6 +109,7 @@ export class UiSettingsClient implements IUiSettingsClient {

async setMany(changes: Record<string, any>) {
this.cache.del();
this.rawCache.del();
this.onWriteHook(changes);
await this.write({ changes });
}
Expand Down Expand Up @@ -143,8 +146,17 @@ export class UiSettingsClient implements IUiSettingsClient {
}

private async getRaw(): Promise<UiSettingsRaw> {
const cachedValue = this.rawCache.get();
if (cachedValue) {
return cachedValue;
}

const userProvided = await this.getUserProvided();
return defaultsDeep({}, userProvided, this.defaults);
const result = defaultsDeep({ ...userProvided }, this.defaults);

this.rawCache.set(result);

return result;
}

private validateKey(key: string, value: unknown) {
Expand Down

0 comments on commit ba59eee

Please sign in to comment.