File tree Expand file tree Collapse file tree 4 files changed +12
-11
lines changed
app/src/main/java/com/im4j/kakacache/rxjava/demo
library/src/main/java/com/im4j/kakacache/rxjava Expand file tree Collapse file tree 4 files changed +12
-11
lines changed Original file line number Diff line number Diff line change 77import android .widget .Button ;
88
99import com .im4j .kakacache .rxjava .netcache .RxRemoteCache ;
10- import com .im4j .kakacache .rxjava .netcache .strategy .FirstTimeStrategy ;
10+ import com .im4j .kakacache .rxjava .netcache .strategy .FirstCacheStrategy ;
1111
1212import java .util .concurrent .TimeUnit ;
1313
14- import rx .Observable ;
15-
1614/**
1715 * Demo主界面
1816 * @version alafighting 2016-06
@@ -29,7 +27,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2927
3028 btnTest = (Button ) findViewById (R .id .btn_test_cache );
3129 btnTest .setOnClickListener (view -> {
32- remote ().compose (RxRemoteCache .transformer (KEY_CACHE , new FirstTimeStrategy ())).subscribe (data -> {
30+ remote ().compose (RxRemoteCache .transformer (KEY_CACHE , new FirstCacheStrategy ())).subscribe (data -> {
3331 Log .e ("main" , "next data=" + data );
3432 }, error -> {
3533 Log .e ("main" , "error" , error );
@@ -40,7 +38,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4038 }
4139
4240 private rx .Observable <String > remote () {
43- Log .e ("main" , "run remote1" );
4441 return rx .Observable .just ("test remote" ).delay (3 , TimeUnit .SECONDS );
4542 }
4643
Original file line number Diff line number Diff line change 11package com .im4j .kakacache .rxjava .manager ;
22
3+ import android .util .Log ;
4+
35import com .im4j .kakacache .rxjava .common .exception .CacheException ;
46import com .im4j .kakacache .rxjava .core .CacheCore ;
57import com .im4j .kakacache .rxjava .core .CacheTarget ;
@@ -41,14 +43,15 @@ public <T> rx.Observable<T> load(final String key) {
4143 return rx .Observable .create (new SimpleSubscribe <T >() {
4244 @ Override
4345 void execute (Subscriber <? super T > subscriber ) {
46+ Log .e ("CacheManager" , "loadCache key=" +key );
4447 T value = _load (key );
4548 subscriber .onNext (value );
4649 }
4750 @ Override
4851 void onError (Subscriber <? super T > subscriber , Throwable e ) {
4952 subscriber .onNext (null );
5053 }
51- }). subscribeOn ( Schedulers . computation ()) ;
54+ });
5255 }
5356
5457 /**
Original file line number Diff line number Diff line change 1515abstract class BasicCacheStrategy <T > implements CacheStrategy {
1616
1717 Observable <ResultData <T >> loadCache (String key ) {
18- Log .e ("Strategy" , "loadCache key=" +key );
19- return RxRemoteCache .load (key ).map (it -> new ResultData <>(ResultFrom .Cache , key , (T ) it ));
18+ return RxRemoteCache .load (key ).map (it -> {
19+ Log .e ("Strategy" , "loadCache result=" +it );
20+ return new ResultData <>(ResultFrom .Cache , key , (T ) it );
21+ });
2022 }
2123
2224 Observable <ResultData <T >> loadRemote (String key , Observable <T > source ) {
2325 return source .map (it -> {
24- Log .e ("Strategy" , "save key=" +key +", value=" +it );
2526 RxRemoteCache .save (key , it ).subscribe (status -> Log .e ("save status" , "status=" +status ) );
2627 return new ResultData <>(ResultFrom .Remote , key , it );
2728 });
Original file line number Diff line number Diff line change 99 * 谁先响应,处理谁
1010 * @version alafighting 2016-06
1111 */
12- public class FirstTimeStrategy <T > extends BasicCacheStrategy <T > {
12+ public class FirstCacheStrategy <T > extends BasicCacheStrategy <T > {
1313
1414 @ Override
1515 public Observable <ResultData <T >> execute (String key , Observable source ) {
1616 return Observable .concat (
1717 loadCache (key ),
1818 loadRemote (key , source )
19- ).first ( it -> ((ResultData <T >) it ) .data != null ).subscribeOn (Schedulers .io ());
19+ ).firstOrDefault ( null , it -> ((ResultData <T >)it .data ) != null ).subscribeOn (Schedulers .io ());
2020 }
2121
2222}
You can’t perform that action at this time.
0 commit comments