react-native-img-cache
react-native-img-cache copied to clipboard
How to get image cache size ?
@chuanfengH Are you referring to image size on disk or the number images in the cache?
@antondomratchev image size on disk . before i use ImageCache.get().clear() , i want to get the total cache size.
这个问题解决了吗?我也想知道在清空缓存之前,如何显示缓存大小,谢谢!
This feature is very useful when user decides to clear the cache.
It's work for me ^_^
import RNFetchBlob from "rn-fetch-blob";
......
bytesToSize(bytes) {
if (bytes === 0) {
return '0 B';
}
let k = 1024;
let sizes = ['B','KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
let i = Math.floor(Math.log(bytes) / Math.log(k));
let num = bytes / Math.pow(k, i);
return num.toPrecision(3) + ' ' + sizes[i];
}
......
componentDidMount() {
console.log(RNFetchBlob.fs.dirs.CacheDir + "/react-native-img-cache");
RNFetchBlob.fs.lstat(RNFetchBlob.fs.dirs.CacheDir + '/react-native-img-cache')
.then((stats) => {
console.log(stats);
let bytes = 0;
stats.map(f => {
bytes += Number(f.size);
});
console.log(this.bytesToSize(bytes));
})
.catch((err) => {
console.log(err);
})
}