@@ -91,6 +91,49 @@ export class ChromeCacheStore<T> implements CacheStore<T> {
91
91
async clear ( regex : string = '' ) {
92
92
return this . store . removeAll ( `^${ this . prefix } :${ regex } ` ) ;
93
93
}
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
+ }
94
137
}
95
138
96
139
export const CachePrefix = {
0 commit comments