我有两个使用共同存储库的片段。
我正在尝试为此存储库实现缓存管理系统。
这个想法是:
其中一个片段被加载,它调用getData()
方法,此方法使用getDataFromNetwork()
对远程JSON Api进行网络调用,获取结果并将其作为{{1}放入缓存中(我的代码中的List<Aqicn>
变量)。
加载下一个片段。如果它在60秒之前发生,那么没有网络呼叫,数据直接来自我的数据列表中的缓存使用data
。
RxJava getDataFromMemory()
用于知道Observable(我的ArrayList)是否为空,并调用正确的方法。
我不知道如何首次亮相,所以我只在主布局上放了一个按钮。当我启动我的应用程序时,第一个片段会自动加载,第一次调用Observable.switchIfEmpty()
。当我按下此按钮时,它会加载第二个片段,第二次调用getData()
。
如果我在60秒之前按下此按钮,我就不应该对JSON api进行网络调用,但是......我有一个,我总是接到第二次网络呼叫并且我的缓存数据没有被使用。我的代码出了什么问题?
getData()
=======编辑:我将我的代码简化为最小值===========
public class CommonRepository implements Repository {
private static final String TAG = CommonRepository.class.getSimpleName();
private long timestamp;
private static final long STALE_MS = 60 * 1000; // Data is stale after 60 seconds
private PollutionApiService pollutionApiService;
private ArrayList<Aqicn> data;
public CommonRepository(PollutionApiService pollutionApiService) {
this.pollutionApiService = pollutionApiService;
this.timestamp = System.currentTimeMillis();
data = new ArrayList<>();
}
@Override
public Observable<Aqicn> getDataFromNetwork(String city, String authToken) {
Observable<Aqicn> aqicn = pollutionApiService.getPollutionObservable(city, authToken)
.doOnNext(new Action1<Aqicn>() {
@Override
public void call(Aqicn aqicn) {
data.add(aqicn);
}
});
return aqicn;
}
private boolean isUpToDate() {
return System.currentTimeMillis() - timestamp < STALE_MS;
}
@Override
public Observable<Aqicn> getDataFromMemory() {
if (isUpToDate()) {
return Observable.from(data);
} else {
timestamp = System.currentTimeMillis();
data.clear();
return Observable.empty();
}
}
@Override
public Observable<Aqicn> getData(String city, String authToken) {
return getDataFromMemory().switchIfEmpty(getDataFromNetwork(city, authToken));
}
}
只是意识到无论我在做什么,当我public class CommonRepository implements Repository {
private PollutionApiService pollutionApiService;
private static Observable<Aqicn> cachedData = null;
public CommonRepository(PollutionApiService pollutionApiService) {
this.pollutionApiService = pollutionApiService;
}
@Override
public Observable<Aqicn> getDataFromNetwork(String city, String authToken) {
Observable<Aqicn> aqicn = pollutionApiService.getPollutionObservable(city, authToken);
cachedData = aqicn;
return aqicn;
}
@Override
public Observable<Aqicn> getData(String city, String authToken) {
if(cachedData == null) {
return getDataFromNetwork(city, authToken);
}
return cachedData;
}
}
进行网络通话时......
=====编辑问题已发现,但未找到解决方案==========
事情是在我的构造函数中我初始化了我的pollutionApiService。 这使用Dagger作为JSON请求并返回一个Observable:
return cachedData
我不知道这一切是如何运作的,但我这样插入。 Dagger创建一个Observable的PollutionApiService提供程序。当我public interface PollutionApiService {
@GET("feed/{city}/")
Observable<Aqicn> getPollutionObservable(@Path("city") String city, @Query("token") String token);
}
订阅此Observable时,网络调用已完成...但不知道如何修复它。事实是每次我return cachedData
都有网络电话。
答案 0 :(得分:1)
我使用以下类实现了缓存行为。
要使用Cache类,您需要具有以下依赖关系:https://cache2k.org/docs/1.0/user-guide.html#android
interface Repository {
Single<Result> getData(String param1, String param2);
}
class RepositoryImpl implements Repository {
private final Cache<String, Result> cache;
private final Function2<String, String, String> calculateKey;
RepositoryImpl(Cache<String, Result> cache) {
this.cache = cache;
this.calculateKey = (s, s2) -> s + s2;
}
@Override
public Single<Result> getData(String param1, String param2) {
Maybe<Result> networkFallback = getFromNetwork(param1, param2, calculateKey).toMaybe();
return getFromCache(param1, param2, calculateKey).switchIfEmpty(networkFallback)
.toSingle();
}
private Single<Result> getFromNetwork(String param1, String param2, Function2<String, String, String> calculateKey) {
return Single.fromCallable(Result::new)
.doOnSuccess(result -> {
if (!cache.containsKey(calculateKey.apply(param1, param2))) {
System.out.println("save in cache");
String apply = calculateKey.apply(param1, param2);
cache.put(apply, result);
}
}) // simulate network request
.delay(50, TimeUnit.MILLISECONDS);
}
private Maybe<Result> getFromCache(String param1, String param2, Function2<String, String, String> calculateKey) {
return Maybe.defer(() -> {
String key = calculateKey.apply(param1, param2);
if (cache.containsKey(key)) {
System.out.println("get from cache");
return Maybe.just(cache.get(key));
} else {
return Maybe.empty();
}
});
}
}
class Result {
}
测试行为:
@Test
// Call getData two times with equal params. First request gets cached. Second request requests from network too, because cash has already expired.
void getData_requestCashed_cashExpiredOnRequest() throws Exception {
// Arrange
Cache<String, Result> cacheMock = mock(Cache.class);
InOrder inOrder = Mockito.inOrder(cacheMock);
Repository rep = new RepositoryImpl(cacheMock);
Result result = new Result();
when(cacheMock.containsKey(anyString())).thenAnswer(invocation -> false);
when(cacheMock.get(anyString())).thenAnswer(invocation -> result);
Single<Result> data1 = rep.getData("hans", "wurst");
Single<Result> data2 = rep.getData("hans", "wurst");
// Action
data1.test()
.await()
.assertValueAt(0, r -> r != result);
// Validate first Subscription: save to cache
inOrder.verify(cacheMock, times(2))
.containsKey(anyString());
inOrder.verify(cacheMock, times(1))
.put(anyString(), any());
data2.test()
.await()
.assertValueAt(0, r -> r != result);
// Validate second Subscription: save to cache
inOrder.verify(cacheMock, times(2))
.containsKey(anyString());
inOrder.verify(cacheMock, times(1))
.put(anyString(), any());
}
@Test
// Call getData two times with different params for each request. Values cashed but only for each request. Second request will hit network again due to different params.
void getData_twoDifferentRequests_cacheNotHit() throws Exception {
// Arrange
Cache<String, Result> cacheMock = mock(Cache.class);
InOrder inOrder = Mockito.inOrder(cacheMock);
Repository rep = new RepositoryImpl(cacheMock);
Result result = new Result();
when(cacheMock.containsKey(anyString())).thenAnswer(invocation -> false);
when(cacheMock.get(anyString())).thenAnswer(invocation -> result);
Single<Result> data1 = rep.getData("hans", "wurst");
Single<Result> data2 = rep.getData("hansX", "wurstX");
// Action
data1.test()
.await()
.assertValueAt(0, r -> r != result);
// Validate first Subscription: save to cache
inOrder.verify(cacheMock, times(2))
.containsKey(anyString());
inOrder.verify(cacheMock, times(1))
.put(anyString(), any());
// Action
data2.test()
.await()
.assertValueAt(0, r -> r != result);
// Validate second Subscription: save to cache
inOrder.verify(cacheMock, times(2))
.containsKey(anyString());
inOrder.verify(cacheMock, times(1))
.put(anyString(), any());
}
@Test
// Call getData two times with equal params. First request hit network. Second request hits cache. Cache does not expire between two requests.
void getData_twoEqualRequests_cacheHitOnSecond() throws Exception {
// Arrange
Cache<String, Result> cacheMock = mock(Cache.class);
InOrder inOrder = Mockito.inOrder(cacheMock);
Repository rep = new RepositoryImpl(cacheMock);
Result result = new Result();
when(cacheMock.containsKey(anyString())).thenAnswer(invocation -> false);
Single<Result> data1 = rep.getData("hans", "wurst");
Single<Result> data2 = rep.getData("hans", "wurst");
// Action
data1.test()
.await();
// Validate first Subscription: save to cache
inOrder.verify(cacheMock, times(2))
.containsKey(anyString());
inOrder.verify(cacheMock, times(0))
.get(anyString());
inOrder.verify(cacheMock, times(1))
.put(anyString(), any());
when(cacheMock.containsKey(anyString())).thenAnswer(invocation -> true);
when(cacheMock.get(anyString())).thenAnswer(invocation -> result);
TestObserver<Result> sub2 = data2.test()
.await()
.assertNoErrors()
.assertValueCount(1)
.assertComplete();
// Validate second subscription: load from cache
inOrder.verify(cacheMock, times(1))
.containsKey(anyString());
inOrder.verify(cacheMock, times(0))
.put(anyString(), any());
inOrder.verify(cacheMock, times(1))
.get(anyString());
sub2.assertResult(result);
}