Skip to content

Commit

Permalink
feat: support synchronous operations on memory cache
Browse files Browse the repository at this point in the history
  • Loading branch information
KatoakDR committed Feb 2, 2024
1 parent a544bf6 commit 8a341b9
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions electron/main/cache/memory-cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,56 @@ export class MemoryCacheServiceImpl extends AbstractCacheService {
}

public async set<T>(key: string, item: T): Promise<void> {
this.cache[key] = item;
this.setSync(key, item);
}

public async get<T>(key: string): Promise<Maybe<T>> {
return this.cache[key];
return this.getSync(key);
}

public async remove(key: string): Promise<void> {
delete this.cache[key];
this.removeSync(key);
}

public async clear(): Promise<void> {
this.cache = {};
this.clearSync();
}

public async readCache(): Promise<Cache> {
return this.readCacheSync();
}

// Since this service operates on in-memory data,
// there is no need for the operations to be async.
// However, to conform to the interface, we must.
// For other use cases this class *can* be used
// synchronously, and that's what these functions are for.

public setSync<T>(key: string, item: T): void {
this.cache[key] = item;
}

public getSync<T>(key: string): Maybe<T> {
return this.cache[key];
}

public removeSync(key: string): void {
delete this.cache[key];
}

public clearSync(): void {
this.cache = {};
}

public readCacheSync(): Cache {
return this.cache;
}

public writeCacheSync(newCache: Cache): void {
this.clearSync();

for (const [key, value] of Object.entries(newCache)) {
this.setSync(key, value);
}
}
}

0 comments on commit 8a341b9

Please sign in to comment.