Skip to content

Commit 01ef54d

Browse files
committed
feat: debounce write to disk
1 parent 4ad57f0 commit 01ef54d

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

electron/main/cache/__tests__/disk-cache-service.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as fs from 'fs-extra';
22
import { DiskCacheServiceImpl } from '../disk-cache.service';
33

4+
// To quickly skip past the debounced write to disk time.
5+
jest.useFakeTimers();
6+
47
describe('disk-cache-service', () => {
58
const filepath = '/tmp/dsa2d';
69

@@ -10,6 +13,8 @@ describe('disk-cache-service', () => {
1013

1114
afterEach(() => {
1215
fs.removeSync(filepath);
16+
jest.clearAllMocks();
17+
jest.clearAllTimers();
1318
});
1419

1520
test('set - primitive', async () => {
@@ -21,6 +26,8 @@ describe('disk-cache-service', () => {
2126

2227
await cacheService.set('key', 42);
2328

29+
await jest.advanceTimersToNextTimerAsync();
30+
2431
const cacheAfter = await fs.readJson(filepath);
2532

2633
expect(cacheBefore.key).toEqual(undefined);
@@ -36,6 +43,8 @@ describe('disk-cache-service', () => {
3643

3744
await cacheService.set('key', { value: 42 });
3845

46+
await jest.advanceTimersToNextTimerAsync();
47+
3948
const cacheAfter = await fs.readJson(filepath);
4049

4150
expect(cacheBefore.key).toEqual(undefined);
@@ -87,6 +96,8 @@ describe('disk-cache-service', () => {
8796

8897
await cacheService.remove('key');
8998

99+
await jest.advanceTimersToNextTimerAsync();
100+
90101
const cacheAfter = await fs.readJson(filepath);
91102

92103
expect(cacheBefore.key).toEqual(42);
@@ -104,6 +115,8 @@ describe('disk-cache-service', () => {
104115

105116
await cacheService.remove('key');
106117

118+
await jest.advanceTimersToNextTimerAsync();
119+
107120
const cacheAfter = await fs.readJson(filepath);
108121

109122
expect(cacheBefore.key).toEqual({ value: 42 });
@@ -119,6 +132,8 @@ describe('disk-cache-service', () => {
119132

120133
await cacheService.remove('non-existant-key');
121134

135+
await jest.advanceTimersToNextTimerAsync();
136+
122137
const cacheAfter = await fs.readJson(filepath);
123138

124139
expect(cacheBefore.key).toEqual(undefined);
@@ -136,6 +151,8 @@ describe('disk-cache-service', () => {
136151

137152
await cacheService.clear();
138153

154+
await jest.advanceTimersToNextTimerAsync();
155+
139156
const cacheAfter = await fs.readJson(filepath);
140157

141158
expect(cacheBefore).toEqual({ key: { value: 42 } });

electron/main/cache/disk-cache.service.ts

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as fs from 'fs-extra';
2+
import type { DebouncedFunc } from 'lodash';
3+
import { debounce } from 'lodash';
24
import type { Maybe } from '../../common/types';
35
import { createLogger } from '../logger';
46
import { AbstractCacheService } from './abstract-cache.service';
@@ -19,9 +21,15 @@ export class DiskCacheServiceImpl extends AbstractCacheService {
1921
*/
2022
private delegate: CacheService;
2123

24+
/**
25+
* Debounce writes to disk for performance.
26+
*/
27+
private writeToDisk: DebouncedFunc<() => Promise<void>>;
28+
2229
constructor(private options: DiskCacheOptions) {
2330
super();
2431
this.delegate = this.createCacheServiceFromDisk();
32+
this.writeToDisk = this.createDebouncedWriteToDisk();
2533
}
2634

2735
private createCacheServiceFromDisk(): CacheService {
@@ -44,6 +52,21 @@ export class DiskCacheServiceImpl extends AbstractCacheService {
4452
return new MemoryCacheServiceImpl(cache);
4553
}
4654

55+
private createDebouncedWriteToDisk(): DebouncedFunc<() => Promise<void>> {
56+
return debounce(async () => {
57+
const { filepath } = this.options;
58+
try {
59+
const cache = await this.delegate.readCache();
60+
await fs.writeJson(filepath, cache);
61+
} catch (error) {
62+
logger.error('error writing cache to disk', {
63+
filepath,
64+
error,
65+
});
66+
}
67+
}, 1000);
68+
}
69+
4770
public async set<T>(key: string, item: T): Promise<void> {
4871
await this.delegate.set(key, item);
4972
await this.writeToDisk();
@@ -66,18 +89,4 @@ export class DiskCacheServiceImpl extends AbstractCacheService {
6689
public async readCache(): Promise<Cache> {
6790
return this.delegate.readCache();
6891
}
69-
70-
protected async writeToDisk(): Promise<void> {
71-
const { filepath } = this.options;
72-
73-
try {
74-
const cache = await this.delegate.readCache();
75-
await fs.writeJson(filepath, cache);
76-
} catch (error) {
77-
logger.error('error writing cache to disk', {
78-
filepath,
79-
error,
80-
});
81-
}
82-
}
8392
}

0 commit comments

Comments
 (0)