Skip to content

Commit e09d9c7

Browse files
committed
feat(cache): clean stale cache on init
1 parent e7aa62a commit e09d9c7

File tree

4 files changed

+82
-38
lines changed

4 files changed

+82
-38
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
"release:changelog": "extract-changelog-release > RELEASE.md"
5252
},
5353
"dependencies": {
54-
"@dvcol/base-http-client": "^1.14.0",
55-
"@dvcol/common-utils": "^1.11.2",
56-
"@dvcol/simkl-http-client": "^1.1.2",
57-
"@dvcol/tmdb-http-client": "^1.3.4",
58-
"@dvcol/trakt-http-client": "^1.4.9",
54+
"@dvcol/base-http-client": "^1.15.2",
55+
"@dvcol/common-utils": "^1.13.1",
56+
"@dvcol/simkl-http-client": "^1.1.6",
57+
"@dvcol/tmdb-http-client": "^1.3.8",
58+
"@dvcol/trakt-http-client": "^1.4.14",
5959
"@dvcol/web-extension-utils": "^3.3.2",
6060
"@vue/devtools": "^7.0.15",
6161
"iso-3166-2": "^1.0.0",

pnpm-lock.yaml

+33-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/trakt.service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ export class TraktService {
728728
ratings: TraktService.traktClient.sync.ratings.get.cached.evict,
729729
stats: TraktService.traktClient.users.stats.cached.evict,
730730
settings: TraktService.traktClient.users.settings.cached.evict,
731+
clean: () => Promise.all([TraktService.caches.trakt.clean(), TraktService.caches.simkl.clean(), TraktService.caches.tmdb.clean()]),
731732
};
732733

733734
static export = {

src/utils/cache.utils.ts

+43
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,49 @@ export class ChromeCacheStore<T> implements CacheStore<T> {
9191
async clear(regex: string = '') {
9292
return this.store.removeAll(`^${this.prefix}:${regex}`);
9393
}
94+
95+
async entries() {
96+
const data = await this.store.getAll<Record<string, CacheStoreEntity<T>>>(`^${this.prefix}:`);
97+
return new Map<string, CacheStoreEntity<T>>(Object.entries(data)).entries();
98+
}
99+
100+
async values() {
101+
const data = await this.store.getAll<Record<string, CacheStoreEntity<T>>>(`^${this.prefix}:`);
102+
return new Map<string, CacheStoreEntity<T>>(Object.entries(data)).values();
103+
}
104+
105+
async keys() {
106+
const data = await this.store.getAll<Record<string, CacheStoreEntity<T>>>(`^${this.prefix}:`);
107+
return new Map<string, CacheStoreEntity<T>>(Object.entries(data)).keys();
108+
}
109+
110+
/**
111+
* Clean the cache by removing all entries that are older than the retention period.
112+
*
113+
* If an entry has an eviction date in the future, it will not be evicted regardless of the retention period.
114+
*
115+
* If no retention period is defined, do nothing.
116+
*
117+
* @param retention The duration in milliseconds after which the cache will be cleared.
118+
*/
119+
async clean(retention = this.retention) {
120+
if (retention === undefined) throw new Error('No retention period defined');
121+
const data = await this.store.getAll<Record<string, CacheStoreEntity<T>>>(`^${this.prefix}:`);
122+
const now = Date.now();
123+
// date before which the cache should be evicted
124+
const expires = now - retention;
125+
await Promise.all(
126+
Object.entries(data)
127+
.map(([key, value]) => {
128+
// if there is an eviction date, and it is in the future, do not evict
129+
if (value.evictAt && value.evictAt > now) return;
130+
// if there is a cached date, and it is after the expiration date, do not evict
131+
if (value.cachedAt && value.cachedAt > expires) return;
132+
return this.store.remove(key);
133+
})
134+
.filter(Boolean),
135+
);
136+
}
94137
}
95138

96139
export const CachePrefix = {

0 commit comments

Comments
 (0)