This repository was archived by the owner on Mar 1, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
packages/plugins/cache/src/providers Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ export class MemoryCacheProvider implements CacheProvider {
1818 for ( const [ key , entry ] of this . entryStore ) {
1919 if ( entryIsExpired ( entry ) ) {
2020 this . entryStore . delete ( key ) ;
21+ this . options ?. onIntervalExpiration ?.( entry ) ;
2122 }
2223 }
2324
@@ -87,4 +88,9 @@ export type MemoryCacheOptions = {
8788 * @default 60
8889 */
8990 checkInterval ?: number ;
91+
92+ /**
93+ * Called when an entry has expired via the interval check.
94+ */
95+ onIntervalExpiration ?: ( entry : CacheEntry ) => void ,
9096} ;
Original file line number Diff line number Diff line change @@ -868,6 +868,37 @@ describe('Cache plugin (memory)', () => {
868868 } ) ;
869869 } ) ;
870870
871+ it ( 'supports custom options' , async ( ) => {
872+ const onIntervalExpiration = vi . fn ( ( ) => { } ) ;
873+ const extDb = db . $use ( defineCachePlugin ( {
874+ provider : new MemoryCacheProvider ( {
875+ checkInterval : 10 ,
876+ onIntervalExpiration,
877+ } ) ,
878+ } ) ) ;
879+
880+ await extDb . user . exists ( {
881+ cache : {
882+ ttl : 5 ,
883+ } ,
884+ } ) ;
885+
886+ vi . advanceTimersByTime ( 5100 ) ;
887+ expect ( onIntervalExpiration ) . not . toHaveBeenCalled ( ) ;
888+ vi . advanceTimersByTime ( 10000 ) ;
889+ expect ( onIntervalExpiration ) . toHaveBeenCalledOnce ( ) ;
890+
891+ // @ts -expect-error
892+ const arg = onIntervalExpiration . mock . lastCall [ 0 ] ;
893+
894+ expect ( arg ) . toMatchObject ( {
895+ result : false ,
896+ options : {
897+ ttl : 5 ,
898+ } ,
899+ } )
900+ } ) ;
901+
871902 it ( 'handles edge cases' , async ( ) => {
872903 const extDb = db . $use ( defineCachePlugin ( {
873904 provider : new MemoryCacheProvider ( ) ,
You can’t perform that action at this time.
0 commit comments