import clc from 'cli-color' ; import fs from 'fs' ; import path from 'path' ; export interface CacheDataInterface { id : string ; confirmed : boolean ; } export default class Cache { private cache : Map < string , CacheDataInterface > ; private cacheFile : string = path . join ( __dirname , '..' , '..' , 'cached.json' ) ; constructor ( public readonly debug = false , public readonly isArLocal ) { if ( this . isArLocal ) { this . cacheFile = path . join ( __dirname , '..' , '..' , 'cached-arlocal.json' ) ; } this . cache = new Map ( ) ; if ( fs . existsSync ( this . cacheFile ) ) { try { const entries = JSON . parse ( fs . readFileSync ( this . cacheFile , 'utf8' ) ) ; for ( const [ key , value ] of entries ) { this . cache . set ( key , value ) ; } } catch ( e ) { } } } public get ( key ) { return this . cache . get ( key ) ; } public set ( key , value ) { this . cache . set ( key , value ) ; } public has ( key ) { return this . cache . has ( key ) ; } public delete ( key ) { this . cache . delete ( key ) ; } public clear ( ) { this . cache . clear ( ) ; } public size ( ) { return this . cache . size ; } public save ( ) { return new Promise ( ( resolve , reject ) => { fs . writeFile ( this . cacheFile , JSON . stringify ( this . entries ( ) ) , 'utf8' , ( err ) => { if ( err ) { if ( this . debug ) { console . log ( clc . red ( 'Error saving cache: ' + err . message ) ) ; reject ( err ) ; } } resolve ( ) ; } ) ; } ) ; } public keys ( ) { return Array . from ( this . cache . keys ( ) ) ; } public values ( ) { return Array . from ( this . cache . values ( ) ) ; } public entries ( ) { return Array . from ( this . cache . entries ( ) ) ; } }