請求網(wǎng)絡(luò)數(shù)據(jù)是在安卓開發(fā)中使用最頻繁的一個(gè)功能伏伯,網(wǎng)絡(luò)請求的體驗(yàn)決定了用戶對整個(gè)APP的感覺呆抑,因此合理地使用緩存對網(wǎng)絡(luò)請求的數(shù)據(jù)進(jìn)行處理極為重要疆瑰。合理的進(jìn)行緩存和網(wǎng)絡(luò)請求是辕,可以為APP帶來更優(yōu)秀的體驗(yàn)囤热。圖片的緩存有Picasso、Glide获三、Fresco等非常著名的框架旁蔼,它們極為成熟并且使用廣泛,程序員應(yīng)該做的是使用輪子而非重復(fù)造輪子疙教。但對于網(wǎng)絡(luò)數(shù)據(jù)的緩存棺聊,大多都是自用自封裝,每個(gè)人都需要進(jìn)行繁瑣的編碼工作贞谓。RxCache就對網(wǎng)絡(luò)緩存進(jìn)行了封裝限佩,并采用RxJava模式,可以與其他RxJava的代碼無縫對接裸弦,使用極為方便祟同。
RxCache使用LruCache和DiskLruCache對網(wǎng)絡(luò)請求數(shù)據(jù)進(jìn)行二級緩存,主要適配于接口API返回?cái)?shù)據(jù)烁兰,不用于圖片等的緩存耐亏。可以設(shè)置緩存模式沪斟、緩存大小广辰,設(shè)置數(shù)據(jù)過期時(shí)間暇矫,并提供了根據(jù)key刪除緩存和清空所有緩存的功能。提供了Gson方式和Serialize方式進(jìn)行數(shù)據(jù)存儲轉(zhuǎn)換與還原择吊。
項(xiàng)目GitHub地址
開始使用:
首先在項(xiàng)目的Gradle中添加依賴:
RxCache使用JitPack進(jìn)行依賴管理李根,所以需要先在項(xiàng)目的build.gradle中添加以下代碼:
allprojects{
repositories{
...
maven{url 'https://jitpack.io'}
}
}
然后在Module的gradle中添加以下依賴:
compile 'com.github.LtLei:RxCache:v1.0.0'
在你的Application中進(jìn)行初始化:
RxCache.init(this);//為RxCache提供Context
也可以使用Builder進(jìn)行高級初始化:
new RxCache.Builder()
.setDebug(true) //開啟debug,開啟后會打印緩存相關(guān)日志几睛,默認(rèn)為true
.setConverter(new GsonConverter()) //設(shè)置轉(zhuǎn)換方式房轿,默認(rèn)為Gson轉(zhuǎn)換
.setCacheMode(CacheMode.BOTH) //設(shè)置緩存模式,默認(rèn)為二級緩存
.setMemoryCacheSizeByMB(50) //設(shè)置內(nèi)存緩存的大小所森,單位是MB
.setDiskCacheSizeByMB(100) //設(shè)置磁盤緩存的大小囱持,單位是MB
.setDiskDirName("RxCache") //設(shè)置磁盤緩存的文件夾名稱
.build();
寫入緩存
RxCache.getInstance()
.put("test", "This is data to cache.", 10 * 1000) //key:緩存的key data:具體的數(shù)據(jù) time:緩存的有效時(shí)間
.compose(RxUtil.<Boolean>io_main()) //線程調(diào)度
.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();
}
});
讀取緩存
讀取緩存時(shí),分為以下幾種情況:
若為Gson轉(zhuǎn)換時(shí):
讀取基本類型數(shù)據(jù)焕济,或自定義的javabean數(shù)據(jù)纷妆,或數(shù)組數(shù)據(jù)等一切可以獲取.class的數(shù)據(jù)
RxCache.getInstance()
.get("test",false,String.class) //key:緩存的key update:表示從緩存獲取數(shù)據(jù)強(qiáng)行返回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的數(shù)據(jù),以上基本數(shù)據(jù)也可以使用此方式
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方式時(shí)掩幢,則統(tǒng)一使用以下方法即可:
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();
}
});