欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

浅谈Android轻量级的数据缓存框架RxCache

程序员文章站 2023-12-15 10:42:04
请求网络数据是在安卓开发中使用最频繁的一个功能,网络请求的体验决定了用户对整个app的感觉,因此合理地使用缓存对网络请求的数据进行处理极为重要。合理的进行缓存和网络请求,可...

请求网络数据是在安卓开发中使用最频繁的一个功能,网络请求的体验决定了用户对整个app的感觉,因此合理地使用缓存对网络请求的数据进行处理极为重要。合理的进行缓存和网络请求,可以为app带来更优秀的体验。图片的缓存有picasso、glide、fresco等非常著名的框架,它们极为成熟并且使用广泛,程序员应该做的是使用*而非重复造*。但对于网络数据的缓存,大多都是自用自封装,每个人都需要进行繁琐的编码工作。rxcache就对网络缓存进行了封装,并采用rxjava模式,可以与其他rxjava的代码无缝对接,使用极为方便。

rxcache使用lrucache和disklrucache对网络请求数据进行二级缓存,主要适配于接口api返回数据,不用于图片等的缓存。可以设置缓存模式、缓存大小,设置数据过期时间,并提供了根据key删除缓存和清空所有缓存的功能。提供了gson方式和serialize方式进行数据存储转换与还原。

项目github地址

rxcache

开始使用:

首先在项目的gradle中添加依赖:

rxcache使用jitpack进行依赖管理,所以需要先在项目的build.gradle中添加以下代码:

allprojects{
  repositories{
    ...
    maven{url 'https://jitpack.io'}
  }
}

然后在module的gradle中添加以下依赖:

compile 'com.github.ltlei:rxcache:v1.0.0'

在你的application中进行初始化:

rxcache.init(this);//为rxcache提供context

也可以使用builder进行高级初始化:

new rxcache.builder()
  .setdebug(true)  //开启debug,开启后会打印缓存相关日志,默认为true
  .setconverter(new gsonconverter()) //设置转换方式,默认为gson转换
  .setcachemode(cachemode.both)  //设置缓存模式,默认为二级缓存
  .setmemorycachesizebymb(50)  //设置内存缓存的大小,单位是mb
  .setdiskcachesizebymb(100)  //设置磁盘缓存的大小,单位是mb
  .setdiskdirname("rxcache")  //设置磁盘缓存的文件夹名称
  .build();

写入缓存

rxcache.getinstance()
  .put("test", "this is data to cache.", 10 * 1000)  //key:缓存的key data:具体的数据 time:缓存的有效时间
  .compose(rxutil.<boolean>io_main()) //线程调度
  .subscribe(new consumer<boolean>() {
    @override
    public void accept(boolean aboolean) throws exception {
      if (aboolean) log.d("cache", "cache successful!");
    }
  },new consumer<throwable>() {
    @override
    public void accept(throwable throwable) throws exception {
      throwable.printstacktrace();
    }
  });

读取缓存

读取缓存时,分为以下几种情况:

若为gson转换时:

读取基本类型数据,或自定义的javabean数据,或数组数据等一切可以获取.class的数据

rxcache.getinstance()
  .get("test",false,string.class)  //key:缓存的key update:表示从缓存获取数据强行返回null
  .compose(rxutil.<cacheresponse<string>>io_main())
  .subscribe(new consumer<cacheresponse<string>>() {
    @override
    public void accept(cacheresponse<string> stringcacheresponse) throws exception {
      if(stringcacheresponse.getdata()!=null)
        log.d("data from cache : "+stringcacheresponse.getdata());
    }
  },new consumer<throwable>() {
    @override
    public void accept(throwable throwable) throws exception {
      throwable.printstacktrace();
    }
  });

读取list等无法获取.class的数据,以上基本数据也可以使用此方式

type type = new typetoken<list<string>>(){}.gettype();
rxcache.getinstance()
  .<list<string>>get("test",false,type)  //由于type不是类,需要指定泛型
  .compose(rxutil.<cacheresponse<list<string>>>io_main())
  .subscribe(new consumer<cacheresponse<list<string>>>() {
    @override
    public void accept(cacheresponse<list<string>> listcacheresponse) throws exception {
      if(listcacheresponse.getdata()!=null)
        log.d("data from cache : "+listcacheresponse.getdata().tostring());
    }
  },new consumer<throwable>() {
    @override
    public void accept(throwable throwable) throws exception {
      throwable.printstacktrace();
    }
  });

若为serialize方式时,则统一使用以下方法即可:

rxcache.getinstance()
  .<list<string>>get("test",false)  //指定泛型,不再需要传.class或type
  .compose(rxutil.<cacheresponse<list<string>>>io_main())
  .subscribe(new consumer<cacheresponse<list<string>>>() {
    @override
    public void accept(cacheresponse<list<string>> listcacheresponse) throws exception {
      if(listcacheresponse.getdata()!=null)
        log.d("data from cache : "+listcacheresponse.getdata().tostring());
    }
  },new consumer<throwable>() {
    @override
    public void accept(throwable throwable) throws exception {
      throwable.printstacktrace();
    }
  });

清除指定缓存

rxcache.getinstance()
  .remove("testlist")
  .compose(rxutil.<boolean>io_main())
  .subscribe(new consumer<boolean>() {
    @override
    public void accept(boolean aboolean) throws exception {
      if (aboolean) log.d("cache data has been deleted.");
    }
  }, new consumer<throwable>() {
    @override
    public void accept(throwable throwable) throws exception {
      throwable.printstacktrace();
    }
  });

清除全部缓存

rxcache.getinstance()
  .clear()
  .compose(rxutil.<boolean>io_main())
  .subscribe(new consumer<boolean>() {
    @override
    public void accept(boolean aboolean) throws exception {
      if (aboolean) log.d("all datas has been deleted.");
    }
  }, new consumer<throwable>() {
    @override
    public void accept(throwable throwable) throws exception {
      throwable.printstacktrace();
    }
  });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: