All Projects → z-chu → Rxcache

z-chu / Rxcache

Licence: apache-2.0
简单一步,缓存搞定。这是一个专用于 RxJava,解决 Android 中对任何 Observable 发出的结果做缓存处理的框架

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Rxcache

Rxanime
Visualizer to understand RxJava operators
Stars: ✭ 261 (-30.77%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Android Kotlin Mvp Architecture
This repository contains a detailed sample app that implements MVP architecture in Kotlin using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 615 (+63.13%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Android Mvp Architecture
This repository contains a detailed sample app that implements MVP architecture using Dagger2, GreenDao, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 4,360 (+1056.5%)
Mutual labels:  rxjava, rxjava2, rxjava-android
RxRetroAPICall
API call example using Retrofit and RxJava2
Stars: ✭ 16 (-95.76%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Grox
Grox helps to maintain the state of Java / Android apps.
Stars: ✭ 336 (-10.88%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Nybus
NYBus (RxBus) - A pub-sub library for Android and Java applications
Stars: ✭ 283 (-24.93%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Traceur
Easier RxJava2 debugging with better stacktraces
Stars: ✭ 502 (+33.16%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Rxjava2 Operators Magician
你用不惯 RxJava,只因缺了这把钥匙 🔑 You are not used to RxJava, just because of the lack of this key.
Stars: ✭ 868 (+130.24%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Mvpframes
整合大量主流开源项目并且可高度配置化的 Android MVP 快速集成框架,支持 AndroidX
Stars: ✭ 100 (-73.47%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Rx.observe
Transform any method to an Rx Observable ! (VIPER)
Stars: ✭ 34 (-90.98%)
Mutual labels:  rxjava, rxjava2, rxjava-android
android-online-course
Android Online Course
Stars: ✭ 22 (-94.16%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Rxjavapriorityscheduler
RxPS - RxJavaPriorityScheduler - A RxJava Priority Scheduler library for Android and Java applications
Stars: ✭ 138 (-63.4%)
Mutual labels:  rxjava, rxjava2, rxjava-android
Freezer
A simple & fluent Android ORM, how can it be easier ? RxJava2 compatible
Stars: ✭ 326 (-13.53%)
Mutual labels:  rxjava, rxjava2, rxjava-android
RxRealm
Utilities for using RxJava with Realm
Stars: ✭ 23 (-93.9%)
Mutual labels:  rxjava, rxjava-android
T Mvp
Android AOP Architecture by Apt, AspectJ, Javassisit, based on Realm+Databinding+MVP+Retrofit+Rxjava2
Stars: ✭ 2,740 (+626.79%)
Mutual labels:  rxjava, rxjava2
RxWebView
RxJava2 binding APIs for Android's WebView
Stars: ✭ 22 (-94.16%)
Mutual labels:  rxjava, rxjava2
Rxandroidble
An Android Bluetooth Low Energy (BLE) Library with RxJava2 interface
Stars: ✭ 3,025 (+702.39%)
Mutual labels:  rxjava, rxjava2
Rxgps
Finding current location cannot be easier on Android !
Stars: ✭ 307 (-18.57%)
Mutual labels:  rxjava, rxjava2
Androidproject
Android 技术中台,但愿人长久,搬砖不再有
Stars: ✭ 4,398 (+1066.58%)
Mutual labels:  rxjava, rxjava2
RxFbLiveVideoEmoticons
Demo of Fb Live Video Reactions using RxJava2
Stars: ✭ 93 (-75.33%)
Mutual labels:  rxjava, rxjava2

RxCache

Download

简单一步,缓存搞定。这是一个专用于 RxJava,解决 Android 中对任何 Observable 发出的结果做缓存处理的框架。

下载APK

特性

缓存层级

  • Observable
  • 内存缓存 - LruCache
  • 磁盘缓存 - DiskLruCache

目前已有的存储策略

  • 优先网络
  • 优先缓存
  • 优先缓存,并设置超时时间
  • 仅加载网络,但数据依然会被缓存
  • 先加载缓存,后加载网络
  • 仅加载网络,不缓存

Getting started

Add the dependencies

allprojects {
    repositories {     
        maven{url 'https://dl.bintray.com/zchu/maven/'} 
    }
}

RxJava 2.0

implementation 'com.zchu:rxcache:2.3.5'

可添加 Kotlin 扩展,解决泛型擦除问题

implementation 'com.zchu:rxcache-kotlin:2.3.5'

首先创建一个 RxCache 实例

rxCache = new RxCache.Builder()
                .appVersion(1)//当版本号改变,缓存路径下存储的所有数据都会被清除掉
                .diskDir(new File(getCacheDir().getPath() + File.separator + "data-cache"))
                .diskConverter(new GsonDiskConverter())//支持Serializable、Json(GsonDiskConverter)
                .memoryMax(2*1024*1024)
                .diskMax(20*1024*1024)
                .build();

再使用 compose()操作符变换, 注意把<~>替换成你的数据类型

observable
	.compose(rxCache.<~>>transformObservable("custom_key", type, CacheStrategy.firstCache()))
	.subscribe(new Observer<CacheResult<~>>() {
		...
		@Override
		public void onNext(CacheResult<~> cacheResult) {
			Object data=cacheResult.getData();//获取你的数据
		}
		...
	}
	

Retrofit

在如果你使用的是 retrofit 那可原有代码的基础上,仅需2行代码搞定,一步到位!!!

Observable 调用

//注意在 <~> 中声明数据源的类型
.compose(rxCache.<~>transformObservablekey,type,CacheStrategy.firstCache()))
.map(new CacheResult.MapFunc<~>())

Flowable 调用

.compose(rxCache.<~>transformFlowablekey,type,CacheStrategy.firstCache()))
.map(new CacheResult.MapFunc<~>())

在这里声明缓存策略即可,不影响原有代码结构

如何你纠结 Key 值的取名,建议使用 ("方法名"+"参数名:"+"加参数值")

CacheStrategy

CacheStrategy 类中提供如下缓存策略:

策略选择 摘要
firstRemote() 优先网络
firstCache() 优先缓存
firstCacheTimeout(milliSecond) 优先缓存,并设置超时时间
onlyRemote() 仅加载网络,但数据依然会被缓存
onlyCache() 仅加载缓存
cacheAndRemote() 先加载缓存,后加载网络
none() 仅加载网络,不缓存

缓存的保存会在数据响应后用异步的方式保存,不会影响数据的响应时间。

如需要用同步方式保存,每个策略都有对应的同步保存方式 如: CacheStrategy.firstRemoteSync() 使用同步保存方式,数据会在缓存写入完以后才响应。

CacheResult

CacheResult 类,包含的属性如下:

public class CacheResult<T> {
    private ResultFrom from;//数据来源,原始observable、内存或硬盘
    private String key;
    private T data; // 数据
    private long timestamp; //数据写入到缓存时的时间戳,如果来自原始observable则为0
	...
}

Default rxCache

你也可以使用默认的 RxCache:
初始化默认的 RxCache

RxCache.initializeDefault(rxcache)

再这样使用

observable
	.compose(RxCache.getDefault().<~>>transformObservable("custom_key", type, strategy))
	...

如果不初始化默认的 RxCache,这样使用缓存会保存到 Environment.getDownloadCacheDirectory()
appVersion 会永远为 1

Kotlin

推荐使用 kotlin ,规避了泛型擦除,可不传 type, 无比简单 :

observable
	.rxCache("custom_key", strategy) //这样会使用默认的 RxCache ,你也可以传入任意 rxcache 使用
	.subscribe(object : Observer<CacheResult<~>>  {
		...
	}
	

泛型

因为泛型擦除的原因,遇到 List<~> 这样的泛型时可以这样使用:

// <~> 为List元素的数据类型
.compose(rxCache.<List<~>>transformer("custom_key", new TypeToken<List<~>>() {}.getType(), strategy))

没有泛型时 Type 直接传 Class 即可

.compose(rxCache.<Bean>transformer("custom_key",Bean.class, strategy))

如果你使用 Kotlin 则没有这个问题

.rxCache(rxcache,"custom_key", strategy)

基础用法

保存缓存:

如 保存字符串到内存和硬盘:

rxCache
	.save("test_key1","RxCache is simple", CacheTarget.MemoryAndDisk)
	.subscribeOn(Schedulers.io())
	.subscribe();

保存方式提供了 3 种选择:

public enum CacheTarget {
    Memory,
    Disk,
    MemoryAndDisk;
...
}

读取缓存:

读取的顺序会按照内存-->硬盘的顺序读取 如 读取缓存中的字符串:

 rxCache
	.<String>load("test_key1", String.class)
	.map(new CacheResult.MapFunc<String>())
	.subscribe(new Consumer<String>() {
		@Override
		public void accept(String value) throws Exception {
			
		}
	});

同步获取缓存:

 CacheResult<String> = rxCache.<String>loadSync("test_key1", String.class);

混淆配置

本 Library 不需求添加额外混淆配置,所以代码都可被混淆

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].