@@ -13,6 +13,7 @@ type TagCache = {
1313 tagCount : number ;
1414} ;
1515
16+ const cacheKeyList : string [ ] = [ ] ;
1617let gameSaved = false ;
1718let tagCache : TagCache | null = null ;
1819
@@ -91,7 +92,6 @@ type GameStringCache = {
9192} ;
9293
9394async function loadStringCache ( key : string , cachePath : string ) : Promise < GameStringCache > {
94- log . info ( 'Cache' , `Loading ${ key } string cache from ${ cachePath } ` ) ;
9595 try {
9696 const cache = await readJsonFile ( cachePath ) ;
9797 if ( typeof cache ?. gameCount !== 'number' ||
@@ -123,8 +123,16 @@ async function loadStringCache(key: string, cachePath: string): Promise<GameStri
123123 }
124124}
125125
126- export function markGameSave ( ) {
126+ export function markGameSave ( fpPath : string ) {
127127 gameSaved = true ;
128+ for ( const key of cacheKeyList ) {
129+ const cachePath = path . join ( fpPath , 'Cache' , `${ key } .json` ) ;
130+ try {
131+ fs . unlinkSync ( cachePath ) ;
132+ } catch {
133+ // Doesn't exist or being edited, ignore
134+ }
135+ }
128136}
129137
130138async function saveStringCache ( cachePath : string , gameCount : number , filterKey : string , data : string [ ] ) {
@@ -141,6 +149,7 @@ function getStringCachedDataFactory(
141149 doSearch : ( search : GameSearch ) => Promise < string [ ] >
142150) : ( state : BackState , tagFilters : TagFilterGroup [ ] ) => Promise < string [ ] > {
143151 let cache : GameStringCache | null = null ;
152+ cacheKeyList . push ( key ) ;
144153
145154 return async ( state , tagFilters ) => {
146155 console . log ( 'Loading cache for ' + key ) ;
@@ -152,20 +161,15 @@ function getStringCachedDataFactory(
152161 cache = await loadStringCache ( key , cachePath ) ;
153162 }
154163
155- console . log ( 'Checked existing cache for ' + key ) ;
156-
157164 return databaseReady ( )
158165 . then ( async ( db ) => {
159166 const gameCount = await db . countGames ( ) ;
160- if ( gameSaved || cache ! . gameCount === gameCount && cache ! . filterKey === flatKey ) {
167+ if ( ! gameSaved && cache ! . gameCount === gameCount && cache ! . filterKey === flatKey ) {
161168 // Same game count and filter key, pretty accurate cache
162- console . log ( 'Returning existing cache for ' + key ) ;
163169 return cache ! . data ;
164170 }
165- console . log ( 'Rebuilding cache for ' + key ) ;
166171 const search = getTaggedSearch ( tagFilters ) ;
167172 const data = await doSearch ( search ) ;
168- console . log ( 'Saving cache for ' + key ) ;
169173 await saveStringCache ( cachePath , gameCount , flatKey , data ) ;
170174 return data ;
171175 } ) ;
0 commit comments