@@ -61,8 +61,11 @@ export function saveImagesToCache(userId: number, chatId: number, images: Conver
6161 timestamp : Date . now ( )
6262 } ;
6363
64+ const jsonSize = JSON . stringify ( cacheData ) . length ;
65+ console . log ( `📸 Saving ${ images . length } images to cache file ${ filePath } (${ Math . round ( jsonSize / 1024 ) } KB)` ) ;
66+
6467 fs . writeFileSync ( filePath , JSON . stringify ( cacheData , null , 2 ) ) ;
65- console . log ( `Saved ${ images . length } images to cache for user ${ userId } in chat ${ chatId } ` ) ;
68+ console . log ( `📸 Successfully saved ${ images . length } images to cache for user ${ userId } in chat ${ chatId } ` ) ;
6669 } catch ( error ) {
6770 console . error ( 'Error saving images to cache:' , error ) ;
6871 }
@@ -75,20 +78,28 @@ export function getImagesFromCache(userId: number, chatId: number): Conversation
7578
7679 try {
7780 if ( ! fs . existsSync ( filePath ) ) {
81+ console . log ( `📸 No cache file found for user ${ userId } in chat ${ chatId } ` ) ;
7882 return [ ] ;
7983 }
8084
8185 const stats = fs . statSync ( filePath ) ;
8286 const now = Date . now ( ) ;
87+ const age = now - stats . mtime . getTime ( ) ;
88+
89+ console . log ( `📸 Cache file age: ${ Math . round ( age / 1000 ) } s (TTL: ${ CACHE_TTL / 1000 } s)` ) ;
8390
8491 // Check if expired
85- if ( now - stats . mtime . getTime ( ) > CACHE_TTL ) {
92+ if ( age > CACHE_TTL ) {
93+ console . log ( `📸 Cache expired, removing file` ) ;
8694 fs . unlinkSync ( filePath ) ;
8795 return [ ] ;
8896 }
8997
9098 const cacheData = JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) ) ;
91- return cacheData . images || [ ] ;
99+ const images = cacheData . images || [ ] ;
100+ console . log ( `📸 Successfully loaded ${ images . length } images from cache` ) ;
101+
102+ return images ;
92103 } catch ( error ) {
93104 console . error ( 'Error reading images from cache:' , error ) ;
94105 return [ ] ;
@@ -102,8 +113,14 @@ export function clearImagesFromCache(userId: number, chatId: number) {
102113
103114 try {
104115 if ( fs . existsSync ( filePath ) ) {
116+ const stats = fs . statSync ( filePath ) ;
117+ const fileSize = Math . round ( stats . size / 1024 ) ;
118+ console . log ( `📸 Clearing cache file ${ filePath } (${ fileSize } KB)` ) ;
119+
105120 fs . unlinkSync ( filePath ) ;
106- console . log ( `Cleared image cache for user ${ userId } in chat ${ chatId } ` ) ;
121+ console . log ( `📸 Successfully cleared image cache for user ${ userId } in chat ${ chatId } ` ) ;
122+ } else {
123+ console . log ( `📸 No cache file to clear for user ${ userId } in chat ${ chatId } ` ) ;
107124 }
108125 } catch ( error ) {
109126 console . error ( 'Error clearing images from cache:' , error ) ;
0 commit comments