1.react-native-http-cache:https://github.com/reactnativecn/react-native-http-cache

2.针对android的问题进行修改

问题:按照文档安装之后,android会报方法getMainDiskStorageCache()找不到

解决办法:
改源码,找到路径node_modules>>react-native-http-cache>>android>>src>>main>>java>>cn>>reactnative>>httpcache>>HttpCacheModule.java
在getImageCacheSize()方法里面 替换这两个方法

 //FileCache cache1 = ImagePipelineFactory.getInstance().getMainDiskStorageCache();
FileCache cache1 = ImagePipelineFactory.getInstance().getMainFileCache();

//FileCache cache2 = ImagePipelineFactory.getInstance().getSmallImageDiskStorageCache();
FileCache cache2 = ImagePipelineFactory.getInstance().getSmallImageFileCache();

React Native清除缓存库react-native-http-cache使用

3.获取缓存大小和清除缓存

  • 获取缓存方法: CacheManager.getCacheSize()
  • 清除缓存方法: CacheManager.clearCache()
/**
 * 获取缓存
 * */
getcache() {
    CacheManager.getCacheSize().then((value) => {
        let size = Math.round((value / 1024 / 1024) * 100) / 100 + 'M';
        console.log(size);
    }, (erro) => {
        console.log(erro)
    });
}

/**
 * 清除缓存
 * */
clean() {
    CacheManager.clearCache();
    this.getcache()
}

相关文章: