allprojects{
repositories{
...
maven{url 'https://jitpack.io'}
}
}
compile 'com.github.LtLei:RxCache:v1.0.0'
RxCache.init(this);//为RxCache提供Context
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();
}
});
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();
}
});
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();
}
});
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();
}
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有