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

作者:i_love_lu 时间:2023-12-22 14:01:30 

请求网络数据是在安卓开发中使用最频繁的一个功能,网络请求的体验决定了用户对整个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();
   }
 });

来源:http://www.jianshu.com/p/d7a25b846a44

标签:Android,RxCache
0
投稿

猜你喜欢

  • Android Flutter实现兴趣标签选择功能

    2021-07-05 14:29:17
  • 基于IDEA中格式化代码的快捷键分享

    2022-08-15 14:58:03
  • SpringBoot异步调用方法实现场景代码实例

    2023-10-23 14:14:23
  • Kotlin中ListView与RecyclerView的应用讲解

    2023-01-24 01:26:44
  • Java Socket使用加密协议进行传输对象的方法

    2023-11-28 12:47:44
  • C++中的auto_ptr智能指针的作用及使用方法详解

    2022-04-07 03:01:10
  • java随机生成8位数授权码的实例

    2022-04-24 12:03:47
  • SpringBoot实现埋点监控

    2022-11-27 06:32:07
  • C#实现Windows Form调用R进行绘图与显示的方法

    2021-11-29 05:40:13
  • springboot整合mybatis将sql打印到日志的实例详解

    2022-06-15 00:56:06
  • 解决使用json-lib包实现xml转json时空值被转为空中括号的问题

    2022-10-20 02:12:14
  • SpringBoot统一处理功能实现的全过程

    2022-12-24 09:43:26
  • 深入了解Spring中的@Autowired和@Resource注解

    2021-09-19 06:57:20
  • SpringBoot @PostConstruct原理用法解析

    2022-02-02 18:05:46
  • Android自定义view实现圆形、圆角和椭圆图片(BitmapShader图形渲染)

    2022-10-08 07:51:35
  • Java String类字符串的理解与认知

    2022-05-10 17:27:12
  • Android布局之帧布局FrameLayout详解

    2023-08-07 04:45:29
  • 详谈Java中的Object、T(泛型)、?区别

    2022-06-11 21:13:11
  • Java 8新增的方法参数反射实例分析

    2021-11-20 05:55:30
  • 解决C#中Linq GroupBy 和OrderBy失效的方法

    2022-11-21 00:25:11
  • asp之家 软件编程 m.aspxhome.com