From 5d64047f87212b98a93d6e981690248c2aa132b7 Mon Sep 17 00:00:00 2001 From: zchu Date: Wed, 11 Oct 2017 10:54:43 +0800 Subject: [PATCH] Add floawable support --- .../java/com/zchu/rxcache/LruMemoryCache.java | 11 +------- .../main/java/com/zchu/rxcache/RxCache.java | 25 +++++++++++-------- .../java/com/zchu/rxcache/RxCacheHelper.java | 4 +-- .../zchu/rxcache/stategy/BaseStrategy.java | 7 +++++- .../stategy/CacheAndRemoteStrategy.java | 14 +++++------ .../rxcache/stategy/FirstCacheStrategy.java | 8 +++--- .../rxcache/stategy/FirstRemoteStrategy.java | 13 +++++----- .../rxcache/stategy/IFlowableStrategy.java | 22 ++++++++++++++++ .../rxcache/stategy/IObservableStrategy.java | 20 +++++++++++++++ .../com/zchu/rxcache/stategy/IStrategy.java | 16 +----------- .../zchu/rxcache/stategy/NoneStrategy.java | 3 +-- .../rxcache/stategy/OnlyCacheStrategy.java | 2 +- .../rxcache/stategy/OnlyRemoteStrategy.java | 6 ++--- .../java/com/zchu/sample/MainActivity.java | 2 +- 14 files changed, 90 insertions(+), 63 deletions(-) create mode 100644 rxcache/src/main/java/com/zchu/rxcache/stategy/IFlowableStrategy.java create mode 100644 rxcache/src/main/java/com/zchu/rxcache/stategy/IObservableStrategy.java diff --git a/rxcache/src/main/java/com/zchu/rxcache/LruMemoryCache.java b/rxcache/src/main/java/com/zchu/rxcache/LruMemoryCache.java index f9890c6..63afa8f 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/LruMemoryCache.java +++ b/rxcache/src/main/java/com/zchu/rxcache/LruMemoryCache.java @@ -8,8 +8,6 @@ import com.zchu.rxcache.utils.MemorySizeOf; import com.zchu.rxcache.utils.Occupy; -import java.io.IOException; -import java.io.Serializable; import java.util.HashMap; /** @@ -78,14 +76,7 @@ private long countSize(Object value) { if (value instanceof Bitmap) { LogUtils.debug("Bitmap"); size = MemorySizeOf.sizeOf((Bitmap) value); - } else if (value instanceof Serializable) { - LogUtils.debug("Serializable"); - try { - size = MemorySizeOf.sizeOf((Serializable) value); - } catch (IOException e) { - size = occupy.occupyof(value); - } - } else { + }else { size = occupy.occupyof(value); } LogUtils.debug("size=" + size + " value=" + value); diff --git a/rxcache/src/main/java/com/zchu/rxcache/RxCache.java b/rxcache/src/main/java/com/zchu/rxcache/RxCache.java index 5df8ad2..d20107d 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/RxCache.java +++ b/rxcache/src/main/java/com/zchu/rxcache/RxCache.java @@ -5,7 +5,8 @@ import com.zchu.rxcache.data.CacheResult; import com.zchu.rxcache.diskconverter.IDiskConverter; import com.zchu.rxcache.diskconverter.SerializableDiskConverter; -import com.zchu.rxcache.stategy.IStrategy; +import com.zchu.rxcache.stategy.IFlowableStrategy; +import com.zchu.rxcache.stategy.IObservableStrategy; import com.zchu.rxcache.utils.LogUtils; import org.reactivestreams.Publisher; @@ -49,12 +50,16 @@ private RxCache(Builder builder) { } + /** + * + * notice: Deprecated! Use {@link #transformObservable(String, Type, IObservableStrategy)} ()} replace. + */ @Deprecated - public ObservableTransformer> transformer(String key, Type type, IStrategy strategy) { + public ObservableTransformer> transformer(String key, Type type, IObservableStrategy strategy) { return transformObservable(key, type, strategy); } - public ObservableTransformer> transformObservable(final String key, final Type type, final IStrategy strategy) { + public ObservableTransformer> transformObservable(final String key, final Type type, final IObservableStrategy strategy) { return new ObservableTransformer>() { @Override public ObservableSource> apply(Observable tObservable) { @@ -63,7 +68,7 @@ public ObservableSource> apply(Observable tObservable) { }; } - public FlowableTransformer> transformFlowable(final String key, final Type type, final IStrategy strategy) { + public FlowableTransformer> transformFlowable(final String key, final Type type, final IFlowableStrategy strategy) { return new FlowableTransformer>() { @Override public Publisher> apply(Flowable flowable) { @@ -92,11 +97,11 @@ public void subscribe(ObservableEmitter observableEmitter) throws Exception { /** * 读取 */ - public Flowable load2(String key, Type type) { - return load2(key, type, BackpressureStrategy.BUFFER); + public Flowable load2Flowable(String key, Type type) { + return load2Flowable(key, type, BackpressureStrategy.LATEST); } - public Flowable load2(final String key, final Type type, BackpressureStrategy backpressureStrategy) { + public Flowable load2Flowable(final String key, final Type type, BackpressureStrategy backpressureStrategy) { return Flowable.create(new FlowableOnSubscribe() { @Override public void subscribe(FlowableEmitter flowableEmitter) throws Exception { @@ -128,14 +133,14 @@ public void subscribe(ObservableEmitter observableEmitter) throws Excep /** * 保存 */ - public Flowable save2(final String key, final T value, final CacheTarget target) { - return save2(key, value, target, BackpressureStrategy.BUFFER); + public Flowable save2Flowable(final String key, final T value, final CacheTarget target) { + return save2Flowable(key, value, target, BackpressureStrategy.LATEST); } /** * 保存 */ - public Flowable save2(final String key, final T value, final CacheTarget target, BackpressureStrategy strategy) { + public Flowable save2Flowable(final String key, final T value, final CacheTarget target, BackpressureStrategy strategy) { return Flowable.create(new FlowableOnSubscribe() { @Override public void subscribe(FlowableEmitter flowableEmitter) throws Exception { diff --git a/rxcache/src/main/java/com/zchu/rxcache/RxCacheHelper.java b/rxcache/src/main/java/com/zchu/rxcache/RxCacheHelper.java index be6687c..f4c8d2b 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/RxCacheHelper.java +++ b/rxcache/src/main/java/com/zchu/rxcache/RxCacheHelper.java @@ -128,7 +128,7 @@ public CacheResult apply(@NonNull Throwable throwable) throws Exception { public static Flowable> loadCacheFlowable(final RxCache rxCache, final String key, Type type, final boolean needEmpty) { Flowable> flowable = rxCache - .load2(key, type) + .load2Flowable(key, type) .flatMap(new Function>>() { @Override public Publisher> apply(@NonNull T t) throws Exception { @@ -212,7 +212,7 @@ public Publisher> apply(@NonNull Throwable throwable) t public static Flowable> saveCacheSyncFlowable(RxCache rxCache, final String key, final T t, CacheTarget target) { return rxCache - .save2(key, t, target) + .save2Flowable(key, t, target) .map(new Function>() { @Override public CacheResult apply(@NonNull Boolean aBoolean) throws Exception { diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/BaseStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/BaseStrategy.java index 5349dca..f1d40e1 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/stategy/BaseStrategy.java +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/BaseStrategy.java @@ -18,9 +18,12 @@ /** * 作者: 赵成柱 on 2016/9/12 0012. + * notice: Deprecated! */ +@Deprecated public abstract class BaseStrategy implements IStrategy { + @Deprecated protected Observable> loadCache(final RxCache rxCache, final String key, Type type, final boolean needEmpty) { Observable> observable = rxCache .load(key, type) @@ -45,6 +48,7 @@ public ObservableSource> apply(@NonNull Throwable throw return observable; } + @Deprecated protected Observable> loadRemote(final RxCache rxCache, final String key, Observable source, final CacheTarget target, final boolean needEmpty) { Observable> observable = source .map(new Function>() { @@ -85,7 +89,7 @@ public ObservableSource> apply(@NonNull Throwable throw return observable; } - + @Deprecated protected Observable> loadRemoteSync(final RxCache rxCache, final String key, Observable source, final CacheTarget target, final boolean needEmpty) { Observable> observable = source .flatMap(new Function>>() { @@ -106,6 +110,7 @@ public ObservableSource> apply(@NonNull Throwable throw } + @Deprecated protected Observable> saveCacheSync(RxCache rxCache, final String key, final T t, CacheTarget target) { return rxCache.save(key, t, target) .map(new Function>() { diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/CacheAndRemoteStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/CacheAndRemoteStrategy.java index df578bb..53df3bc 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/stategy/CacheAndRemoteStrategy.java +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/CacheAndRemoteStrategy.java @@ -18,7 +18,7 @@ * 先缓存,后网络 * 作者: 赵成柱 on 2016/9/12 0012. */ -public final class CacheAndRemoteStrategy extends BaseStrategy { +public final class CacheAndRemoteStrategy implements IStrategy { private boolean isSync; public CacheAndRemoteStrategy() { @@ -31,12 +31,12 @@ public CacheAndRemoteStrategy(boolean isSync) { @Override public Observable> execute(RxCache rxCache, String key, Observable source, Type type) { - Observable> cache = loadCache(rxCache, key, type,true); + Observable> cache = RxCacheHelper.loadCache(rxCache, key, type, true); Observable> remote; if (isSync) { - remote = loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk,false); + remote = RxCacheHelper.loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } else { - remote = loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk,false); + remote = RxCacheHelper.loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } return Observable.concat(cache, remote) .filter(new Predicate>() { @@ -49,12 +49,12 @@ public boolean test(@NonNull CacheResult result) throws Exception { @Override public Publisher> flow(RxCache rxCache, String key, Flowable source, Type type) { - Flowable> cache = RxCacheHelper.loadCacheFlowable(rxCache, key, type,true); + Flowable> cache = RxCacheHelper.loadCacheFlowable(rxCache, key, type, true); Flowable> remote; if (isSync) { - remote = RxCacheHelper.loadRemoteSyncFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk,false); + remote = RxCacheHelper.loadRemoteSyncFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } else { - remote =RxCacheHelper.loadRemoteFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk,false); + remote = RxCacheHelper.loadRemoteFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } return Flowable.concat(cache, remote) .filter(new Predicate>() { diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/FirstCacheStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/FirstCacheStrategy.java index d12e3eb..149c174 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/stategy/FirstCacheStrategy.java +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/FirstCacheStrategy.java @@ -16,7 +16,7 @@ * 优先缓存 * 作者: 赵成柱 on 2016/9/12 0012. */ -public final class FirstCacheStrategy extends BaseStrategy { +public final class FirstCacheStrategy implements IStrategy { private boolean isSync; public FirstCacheStrategy() { @@ -29,12 +29,12 @@ public FirstCacheStrategy(boolean isSync) { @Override public Observable> execute(RxCache rxCache, String key, Observable source, Type type) { - Observable> cache = loadCache(rxCache, key, type,true); + Observable> cache = RxCacheHelper.loadCache(rxCache, key, type,true); Observable> remote; if (isSync) { - remote = loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk,false); + remote = RxCacheHelper.loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk,false); } else { - remote = loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk,false); + remote = RxCacheHelper.loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk,false); } return cache.switchIfEmpty(remote); } diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/FirstRemoteStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/FirstRemoteStrategy.java index 2122d15..d9e380e 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/stategy/FirstRemoteStrategy.java +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/FirstRemoteStrategy.java @@ -17,7 +17,7 @@ * 优先网络 * 作者: 赵成柱 on 2016/9/12 0012. */ -public final class FirstRemoteStrategy extends BaseStrategy { +public final class FirstRemoteStrategy implements IStrategy { private boolean isSync; public FirstRemoteStrategy() { @@ -30,15 +30,15 @@ public FirstRemoteStrategy(boolean isSync) { @Override public Observable> execute(RxCache rxCache, String key, Observable source, Type type) { - Observable> cache = loadCache(rxCache, key, type, true); + Observable> cache = RxCacheHelper.loadCache(rxCache, key, type, true); Observable> remote; if (isSync) { - remote = loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk, false); + remote = RxCacheHelper.loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } else { - remote = loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk, false); + remote = RxCacheHelper.loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } return Observable - .concatDelayError(Arrays.asList(cache,remote)) + .concatDelayError(Arrays.asList(remote,cache)) .take(1); } @@ -49,11 +49,10 @@ public Publisher> flow(RxCache rxCache, String key, Flowable< if (isSync) { remote = RxCacheHelper.loadRemoteSyncFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } else { - remote =RxCacheHelper.loadRemoteFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } return Flowable - .concatDelayError(Arrays.asList(cache,remote)) + .concatDelayError(Arrays.asList(remote,cache)) .take(1); } diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/IFlowableStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/IFlowableStrategy.java new file mode 100644 index 0000000..7c64c0d --- /dev/null +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/IFlowableStrategy.java @@ -0,0 +1,22 @@ +package com.zchu.rxcache.stategy; + + +import com.zchu.rxcache.RxCache; +import com.zchu.rxcache.data.CacheResult; + +import org.reactivestreams.Publisher; + +import java.lang.reflect.Type; + +import io.reactivex.Flowable; + + +/** + * author : zchu + * date : 2017/10/11 + * desc : + */ +public interface IFlowableStrategy { + + Publisher> flow(RxCache rxCache, String key, Flowable source, Type type); +} diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/IObservableStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/IObservableStrategy.java new file mode 100644 index 0000000..6e11dd6 --- /dev/null +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/IObservableStrategy.java @@ -0,0 +1,20 @@ +package com.zchu.rxcache.stategy; + + +import com.zchu.rxcache.RxCache; +import com.zchu.rxcache.data.CacheResult; + +import java.lang.reflect.Type; + +import io.reactivex.Observable; + + +/** + * author : zchu + * date : 2017/10/11 + * desc : + */ +public interface IObservableStrategy { + + Observable> execute(RxCache rxCache, String key, Observable source, Type type); +} diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/IStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/IStrategy.java index fcfb9c8..325a2b0 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/stategy/IStrategy.java +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/IStrategy.java @@ -1,23 +1,9 @@ package com.zchu.rxcache.stategy; -import com.zchu.rxcache.RxCache; -import com.zchu.rxcache.data.CacheResult; - -import org.reactivestreams.Publisher; - -import java.lang.reflect.Type; - -import io.reactivex.Flowable; -import io.reactivex.Observable; - - /** * 作者: 赵成柱 on 2016/9/12 0012. */ -public interface IStrategy { - - Observable> execute(RxCache rxCache, String key, Observable source, Type type); +public interface IStrategy extends IObservableStrategy,IFlowableStrategy { - Publisher> flow(RxCache rxCache, String key, Flowable source, Type type); } diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/NoneStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/NoneStrategy.java index 23c6354..6d7b303 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/stategy/NoneStrategy.java +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/NoneStrategy.java @@ -19,7 +19,7 @@ * 仅加载网络,不缓存 */ -public final class NoneStrategy implements IStrategy { +public final class NoneStrategy implements IStrategy { private NoneStrategy() { } @@ -28,7 +28,6 @@ private NoneStrategy() { @Override public Observable> execute(RxCache rxCache, final String key, Observable source, Type type) { - return source.map(new Function>() { @Override public CacheResult apply(@NonNull T t) throws Exception { diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/OnlyCacheStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/OnlyCacheStrategy.java index 3890ccb..2bb20e0 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/stategy/OnlyCacheStrategy.java +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/OnlyCacheStrategy.java @@ -16,7 +16,7 @@ * 仅加载缓存 * 作者: 赵成柱 on 2016/9/12 0012. */ -public final class OnlyCacheStrategy extends BaseStrategy { +public final class OnlyCacheStrategy implements IStrategy { @Override public Observable> execute(RxCache rxCache, String key, Observable source, Type type) { diff --git a/rxcache/src/main/java/com/zchu/rxcache/stategy/OnlyRemoteStrategy.java b/rxcache/src/main/java/com/zchu/rxcache/stategy/OnlyRemoteStrategy.java index a651888..4862f98 100644 --- a/rxcache/src/main/java/com/zchu/rxcache/stategy/OnlyRemoteStrategy.java +++ b/rxcache/src/main/java/com/zchu/rxcache/stategy/OnlyRemoteStrategy.java @@ -17,7 +17,7 @@ * 仅加载网络,但数据依然会被缓存 * 作者: 赵成柱 on 2016/9/12 0012. */ -class OnlyRemoteStrategy extends BaseStrategy { +class OnlyRemoteStrategy implements IStrategy { private boolean isSync; public OnlyRemoteStrategy() { @@ -31,9 +31,9 @@ public OnlyRemoteStrategy(boolean isSync) { @Override public Observable> execute(RxCache rxCache, String key, Observable source, Type type) { if (isSync) { - return loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk, false); + return RxCacheHelper.loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } else { - return loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk, false); + return RxCacheHelper.loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk, false); } } diff --git a/sample/src/main/java/com/zchu/sample/MainActivity.java b/sample/src/main/java/com/zchu/sample/MainActivity.java index 5574d50..426264b 100644 --- a/sample/src/main/java/com/zchu/sample/MainActivity.java +++ b/sample/src/main/java/com/zchu/sample/MainActivity.java @@ -115,7 +115,7 @@ public List apply(@NonNull GankBean gankBean) throws Excep return gankBean.getResults(); } }) - .compose(rxCache.>transformer("custom_key", new TypeToken>() { + .compose(rxCache.>transformObservable("custom_key", new TypeToken>() { }.getType(), strategy)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())