- 获取缓存大小
static async getCacheSize(context:Context): Promise<CacheInfo>{ let cacheInfo = new CacheInfo(); // let cacheSize: number = 0; let cacheSizeStr: string = "0B"; let bundleStats = await storageStatistics.getCurrentBundleStats(); let cacheSize = bundleStats.cacheSize; console.log("==================cacheDir>>>>>>>>>"+cacheSize); if(cacheSize > (1024*1024*1024)) { cacheSizeStr = (cacheSize/(1024*1024*1024)).toFixed(2) + "G"; }else if(cacheSize > (1024*1024) && cacheSize < (1024*1024*1024)){ cacheSizeStr = (cacheSize/(1024*1024)).toFixed(2) + "M"; }else if (cacheSize > 1024 && cacheSize < 1024*1024) { cacheSizeStr = (cacheSize/1024).toFixed(2) + "K"; }else { cacheSizeStr = cacheSize + "B"; } cacheInfo.cache_size = cacheSizeStr; // 单位B let applicationContext = context.getApplicationContext(); let cacheDir = applicationContext.cacheDir; cacheInfo.cache_path = cacheDir; return cacheInfo; }
class CacheInfo { cache_path: string = ""; cache_size: string = ""; }
2、清理缓存的方法
static clearCache(context:Context){ let applicationContext = context.getApplicationContext(); let cacheDir = applicationContext.cacheDir; console.log("==================cacheDir>>>>>>>>>"+cacheDir); fs.rmdirSync(cacheDir) }