清除缓存:react-native-http-cache

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

android:

npm install react-native-http-cache --save

react-native link react-native-http-cache

 

Android 修改 HttpCacheModule.java 文件

将69、80行分别改为:
FileCache cache1 = ImagePipelineFactory.getInstance().getMainFileCache();
FileCache cache2 = ImagePipelineFactory.getInstance().getSmallImageFileCache();
iOS 修改

 

CacheManager.getCacheSize()    返回网络缓存大小和图片缓存大小

CacheManager.getImageCacheSize()    获取图片缓存 返回的是一个 promise 对象

CacheManager.getHttpCacheSize()    获取网络缓存 返回的是一个 promise 对象

CacheManager.clearImageCache()    清除图片缓存 返回的是一个 promise 对象

CacheManager.clearHttpCache()    清除网络缓存 返回的是一个 promise 对象

CacheManager.clearCache()     清除缓存

CacheManager.getImageCacheSize()
    .then(data=> {
        data = data / (1024 * 1024);
        this.setState({
            imageCacheSize: data.toFixed(2) + 'M',
        })
        console.log('图片缓存' + data)
    })
    .catch(err => {
        console.log(err)
    })
 

 

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Image} from 'react-native';
 
import * as CacheManager from 'react-native-http-cache';
 
export default class Cache extends Component {
    constructor(props){
        super(props);
        this.state = {
            imageCacheSize: 0,
            httpCacheSize: 0,
            size: 0,
        };
    }
    componentDidMount(){
        CacheManager.getImageCacheSize()
            .then(data=> {
                data = data / (1024 * 1024);
                this.setState({
                    imageCacheSize: data.toFixed(2),
                })
                console.log('图片缓存' + data)
            })
            .catch(err => {
                console.log(err)
            })
 
        CacheManager.getHttpCacheSize()
            .then(data=> {
                data = data / (1024 * 1024);
                this.setState({
                    httpCacheSize: data.toFixed(2),
                })
                console.log('网络缓存' + data)
            })
            .catch(err => {
                console.log(err)
            })
    }
    render() {
        let { imageCacheSize,httpCacheSize,size } = this.state;
        size = (imageCacheSize * 1 + httpCacheSize * 1).toFixed(2);
        // this.setState({size:size})
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>Welcome to React Native!</Text>
                <Text style={styles.welcome}>图片缓存大小:{imageCacheSize}M</Text>
                <Text style={styles.welcome}>网络缓存大小:{httpCacheSize}M</Text>
                <Text style={styles.welcome}>缓存:{size}M</Text>
                <View style={{flexDirection:'row',padding: 10,justifyContent:'space-around'}}>
                    <Image 
                        style={{width: 80,height: 80,marginRight:10}}
                        source={{uri:"http://b.hiphotos.baidu.com/image/h%3D300/sign=5a28a3caf1039245beb5e70fb795a4a8/b8014a90f603738d30915925be1bb051f919ecda.jpg"}}
                    />
                    <Image 
                        style={{width: 80,height: 80,marginRight:10}}
                        source={{uri:"http://b.hiphotos.baidu.com/image/h%3D300/sign=b48b76f776899e51678e3c1472a6d990/e824b899a9014c08ef778daf077b02087bf4f468.jpg"}}
                    />
                    <Image 
                        style={{width: 80,height: 80}}
                        source={{uri:"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2723310483,865690233&fm=26&gp=0.jpg"}}
                    />
                </View>
                <Text style={styles.welcome}
                    onPress={() => {
                        CacheManager.clearImageCache()
                            .then(data => {
                                if(data == null){
                                    console.log(data)
                                    this.setState({imageCacheSize: '0.00M'})
                                }
                            })
                            .catch(err => {
                                console.log(err)
                            })
                    }}
                >
                    清除图片缓存
                </Text>
                 <Text style={styles.welcome}
                    onPress={() => {
                        CacheManager.clearHttpCache()
                            .then(data => {
                                if(data == null){
                                    console.log(data)
                                    this.setState({httpCacheSize: '0.00M'})
                                }
                            })
                            .catch(err => {
                                console.log(err)
                            })
                    }}
                >
                    清除网络缓存
                </Text>
                <Text style={styles.welcome}
                    onPress={() => {
                        CacheManager.clearCache();
                        this.setState({
                            imageCacheSize: '0.00',
                            httpCacheSize: '0.00',
                            size: '0.00',
                        })
                    }}
                >
                    清除缓存
                </Text>
            </View>
        );
    }
}
 
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
        paddingTop: Platform.OS == 'ios' ? 35 : null,
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
});
清除缓存的话,有些会没有清除完,可以直接写死,反正缓存不大
--------------------- 
作者:qq_39910762 
来源:CSDN 
原文:https://blog.csdn.net/qq_39910762/article/details/82970348 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值