Skip to content

Commit 1551d1f

Browse files
committed
feat(cache): create chrome storage cache
1 parent fb5afa8 commit 1551d1f

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

src/services/common/base-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export abstract class BaseClient<
238238
const fn: ClientEndpointCall = (param, init) => this._call(template, param, init);
239239

240240
const cachedFn: ClientEndpointCache = async (param, init, cacheOptions) => {
241-
const key = JSON.stringify({ param, init });
241+
const key = JSON.stringify({ endpoint, param, init });
242242
if (!cacheOptions?.force) {
243243
const cached = await this._cache.get(key);
244244
if (cached) {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import type { TmdbApiResponse } from '~/models/tmdb/tmdb-client.model';
2+
13
import { TmdbClient } from '~/services/tmdb-client/clients/tmdb-client';
24
import { tmdbClientSettings } from '~/settings/tmdb.api';
5+
import { ChromeCacheStore } from '~/utils/cache.utils';
36

4-
export const tmdbService = new TmdbClient(tmdbClientSettings);
7+
const cacheStore = new ChromeCacheStore<TmdbApiResponse>({ prefix: 'tmdb-cache' });
8+
export const tmdbService = new TmdbClient({ ...tmdbClientSettings, cacheStore });
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import type { TraktApiResponse } from '~/models/trakt/trakt-client.model';
2+
3+
import { traktApi } from '~/services/trakt-client/api/trakt-api.endpoints';
14
import { TraktClient } from '~/services/trakt-client/clients/trakt-client';
25
import { traktClientSettings } from '~/settings/traktv.api';
6+
import { ChromeCacheStore } from '~/utils/cache.utils';
37

4-
export const traktService = new TraktClient(traktClientSettings);
8+
const cacheStore = new ChromeCacheStore<TraktApiResponse>({ prefix: 'trakt-cache' });
9+
export const traktService = new TraktClient({ ...traktClientSettings, cacheStore }, {}, traktApi);
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import type { TvdbApiResponse } from '~/models/tvdb/tvdb-client.model';
2+
13
import { TvdbClient } from '~/services/tvdb-client/clients/tvdb-client';
24
import { tvdbClientSettings } from '~/settings/tvdb.api';
5+
import { ChromeCacheStore } from '~/utils/cache.utils';
36

4-
export const tvdbService = new TvdbClient(tvdbClientSettings);
7+
const cacheStore = new ChromeCacheStore<TvdbApiResponse>({ prefix: 'tmdb-cache' });
8+
export const tvdbService = new TvdbClient({ ...tvdbClientSettings, cacheStore });

src/utils/browser/browser-storage.utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export const storageWrapper = (area: chrome.storage.StorageArea, name = crypto.r
4444
};
4545
};
4646

47+
export type StorageArea = ReturnType<typeof storageWrapper>;
48+
4749
/**
4850
* This object is used to access the storage areas.
4951
*/

src/utils/cache.utils.ts

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { storage, type StorageArea } from '~/utils/browser/browser-storage.utils';
2+
13
export type CacheStoreEntity<T = unknown> = {
24
value: T;
35
cachedAt: number;
@@ -12,3 +14,41 @@ export type CacheStore<T = unknown> = {
1214
/** the duration in milliseconds after which the cache will be cleared */
1315
retention?: number;
1416
};
17+
18+
export class ChromeCacheStore<T> implements CacheStore<T> {
19+
retention?: number;
20+
store: StorageArea;
21+
prefix: string;
22+
23+
constructor({
24+
retention = 24 * 60 * 60,
25+
store = storage.local,
26+
prefix = 'http-cache',
27+
}: {
28+
retention?: number;
29+
store?: StorageArea;
30+
prefix?: string;
31+
}) {
32+
this.retention = retention;
33+
this.store = store;
34+
this.prefix = prefix;
35+
}
36+
37+
async get(key: string) {
38+
return this.store.get<CacheStoreEntity<T>>(`${this.prefix}:${key}`);
39+
}
40+
41+
async set(key: string, value: CacheStoreEntity<T>) {
42+
await this.store.set(`${this.prefix}:${key}`, value);
43+
return this;
44+
}
45+
46+
async delete(key: string) {
47+
await this.store.remove(`${this.prefix}:${key}`);
48+
return true;
49+
}
50+
51+
async clear() {
52+
return this.store.clear();
53+
}
54+
}

0 commit comments

Comments
 (0)