@@ -54,6 +54,60 @@ class CacheRegistryDatabase {
5454 return this . #performOperation( STORE_NAME , getOlderThanOp , "readonly" )
5555 }
5656
57+ getEntryCount ( cacheName ) {
58+ const countOp = ( store ) => {
59+ const index = store . index ( "cacheNameAndTimestamp" )
60+ const range = IDBKeyRange . bound (
61+ [ cacheName , 0 ] ,
62+ [ cacheName , Infinity ]
63+ )
64+ return this . #requestToPromise( index . count ( range ) )
65+ }
66+ return this . #performOperation( STORE_NAME , countOp , "readonly" )
67+ }
68+
69+ getTotalSize ( cacheName ) {
70+ const sumOp = ( store ) => {
71+ const index = store . index ( "cacheNameAndTimestamp" )
72+ const range = IDBKeyRange . bound (
73+ [ cacheName , 0 ] ,
74+ [ cacheName , Infinity ]
75+ )
76+ const cursorRequest = index . openCursor ( range )
77+
78+ return this . #sumSizesFromCursor( cursorRequest )
79+ }
80+ return this . #performOperation( STORE_NAME , sumOp , "readonly" )
81+ }
82+
83+ getOldestEntries ( cacheName , limit ) {
84+ const getOldestOp = ( store ) => {
85+ const index = store . index ( "cacheNameAndTimestamp" )
86+ const range = IDBKeyRange . bound (
87+ [ cacheName , 0 ] ,
88+ [ cacheName , Infinity ]
89+ )
90+ const cursorRequest = index . openCursor ( range )
91+
92+ return this . #cursorRequestToPromiseWithLimit( cursorRequest , limit )
93+ }
94+ return this . #performOperation( STORE_NAME , getOldestOp , "readonly" )
95+ }
96+
97+ getEntriesForSizeReduction ( cacheName , targetReduction ) {
98+ const getEntriesOp = ( store ) => {
99+ const index = store . index ( "cacheNameAndTimestamp" )
100+ const range = IDBKeyRange . bound (
101+ [ cacheName , 0 ] ,
102+ [ cacheName , Infinity ]
103+ )
104+ const cursorRequest = index . openCursor ( range )
105+
106+ return this . #getEntriesUntilSizeReached( cursorRequest , targetReduction )
107+ }
108+ return this . #performOperation( STORE_NAME , getEntriesOp , "readonly" )
109+ }
110+
57111 delete ( key ) {
58112 const deleteOp = ( store ) => this . #requestToPromise( store . delete ( key ) )
59113 return this . #performOperation( STORE_NAME , deleteOp , "readwrite" )
@@ -101,6 +155,62 @@ class CacheRegistryDatabase {
101155 request . onerror = ( ) => reject ( request . error )
102156 } )
103157 }
158+
159+ #cursorRequestToPromiseWithLimit( request , limit ) {
160+ return new Promise ( ( resolve , reject ) => {
161+ const results = [ ]
162+
163+ request . onsuccess = ( event ) => {
164+ const cursor = event . target . result
165+ if ( cursor && results . length < limit ) {
166+ results . push ( cursor . value )
167+ cursor . continue ( )
168+ } else {
169+ resolve ( results )
170+ }
171+ }
172+
173+ request . onerror = ( ) => reject ( request . error )
174+ } )
175+ }
176+
177+ #sumSizesFromCursor( request ) {
178+ return new Promise ( ( resolve , reject ) => {
179+ let total = 0
180+
181+ request . onsuccess = ( event ) => {
182+ const cursor = event . target . result
183+ if ( cursor ) {
184+ total += cursor . value . size ?? 0
185+ cursor . continue ( )
186+ } else {
187+ resolve ( total )
188+ }
189+ }
190+
191+ request . onerror = ( ) => reject ( request . error )
192+ } )
193+ }
194+
195+ #getEntriesUntilSizeReached( request , targetSize ) {
196+ return new Promise ( ( resolve , reject ) => {
197+ const results = [ ]
198+ let accumulated = 0
199+
200+ request . onsuccess = ( event ) => {
201+ const cursor = event . target . result
202+ if ( cursor && accumulated < targetSize ) {
203+ results . push ( cursor . value )
204+ accumulated += cursor . value . size ?? 0
205+ cursor . continue ( )
206+ } else {
207+ resolve ( results )
208+ }
209+ }
210+
211+ request . onerror = ( ) => reject ( request . error )
212+ } )
213+ }
104214}
105215
106216let cacheRegistryDatabase = null
@@ -138,6 +248,22 @@ export class CacheRegistry {
138248 return this . database . getOlderThan ( this . cacheName , timestamp )
139249 }
140250
251+ getEntryCount ( ) {
252+ return this . database . getEntryCount ( this . cacheName )
253+ }
254+
255+ getTotalSize ( ) {
256+ return this . database . getTotalSize ( this . cacheName )
257+ }
258+
259+ getOldestEntries ( limit ) {
260+ return this . database . getOldestEntries ( this . cacheName , limit )
261+ }
262+
263+ getEntriesForSizeReduction ( targetReduction ) {
264+ return this . database . getEntriesForSizeReduction ( this . cacheName , targetReduction )
265+ }
266+
141267 delete ( key ) {
142268 return this . database . delete ( key )
143269 }
0 commit comments