Search in sources :

Example 1 with ConfigProvider

use of io.rx_cache2.ConfigProvider in project RxCache by VictorAlbertos.

the class ProxyTranslator method loadConfigProviderMethod.

private ConfigProvider loadConfigProviderMethod(Method method) {
    ConfigProvider result;
    synchronized (configProviderMethodCache) {
        result = configProviderMethodCache.get(method);
        if (result == null) {
            result = new ConfigProvider(getProviderKey(method), null, getLifeTimeCache(method), requiredDetailResponse(method), getExpirable(method), isEncrypted(method), null, null, null, null);
            configProviderMethodCache.put(method, result);
        }
    }
    return result;
}
Also used : ConfigProvider(io.rx_cache2.ConfigProvider)

Example 2 with ConfigProvider

use of io.rx_cache2.ConfigProvider in project RxCache by VictorAlbertos.

the class ProxyTranslator method processMethod.

ConfigProvider processMethod(Method method, Object[] objectsMethod) {
    ConfigProvider prev = loadConfigProviderMethod(method);
    ConfigProvider configProvider = new ConfigProvider(prev.getProviderKey(), null, prev.getLifeTimeMillis(), prev.requiredDetailedResponse(), prev.isExpirable(), prev.isEncrypted(), getDynamicKey(method, objectsMethod), getDynamicKeyGroup(method, objectsMethod), getLoaderObservable(method, objectsMethod), evictProvider(method, objectsMethod));
    return configProvider;
}
Also used : ConfigProvider(io.rx_cache2.ConfigProvider)

Example 3 with ConfigProvider

use of io.rx_cache2.ConfigProvider in project RxCache by VictorAlbertos.

the class ProcessorProvidersBehaviour method getDataFromLoader.

private Observable<Reply> getDataFromLoader(final io.rx_cache2.ConfigProvider configProvider, final Record record) {
    return configProvider.getLoaderObservable().map(new Function<Object, Reply>() {

        @Override
        public Reply apply(Object data) throws Exception {
            boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
            if (data == null && useExpiredData && record != null) {
                return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
            }
            clearKeyIfNeeded(configProvider);
            if (data == null) {
                throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey());
            }
            twoLayersCache.save(configProvider.getProviderKey(), configProvider.getDynamicKey(), configProvider.getDynamicKeyGroup(), data, configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
            return new Reply(data, Source.CLOUD, configProvider.isEncrypted());
        }
    }).onErrorReturn(new Function<Object, Object>() {

        @Override
        public Object apply(Object o) throws Exception {
            clearKeyIfNeeded(configProvider);
            boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
            if (useExpiredData && record != null) {
                return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
            }
            throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey(), (Throwable) o);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) Reply(io.rx_cache2.Reply)

Example 4 with ConfigProvider

use of io.rx_cache2.ConfigProvider in project RxCache by VictorAlbertos.

the class ProcessorProvidersTest method getSubscriberCompleted.

private TestObserver getSubscriberCompleted(boolean hasCache, final boolean evictCache, boolean detailResponse, Loader loader, boolean useExpiredDataIfLoaderNotAvailable) {
    Observable observable;
    switch(loader) {
        case VALID:
            observable = Observable.just(new Mock("message"));
            break;
        case NULL:
            observable = Observable.error(new RuntimeException("No data"));
            break;
        default:
            observable = Observable.create(new ObservableOnSubscribe<Object>() {

                @Override
                public void subscribe(ObservableEmitter<Object> e) throws Exception {
                    throw new RuntimeException("error");
                }
            });
            break;
    }
    ConfigProvider configProvider = new ConfigProvider("mockKey", null, null, detailResponse, true, false, "", "", observable, new EvictProvider(evictCache));
    if (hasCache)
        twoLayersCacheMock.save("mockKey", "", "", new Mock("message"), configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
    processorProvidersUT = new ProcessorProvidersBehaviour(twoLayersCacheMock, useExpiredDataIfLoaderNotAvailable, evictExpiredRecordsPersistence, getDeepCopy, doMigrations);
    TestObserver observerMock = processorProvidersUT.getData(configProvider).test();
    observerMock.awaitTerminalEvent();
    return observerMock;
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ConfigProvider(io.rx_cache2.ConfigProvider) EvictProvider(io.rx_cache2.EvictProvider) Observable(io.reactivex.Observable) ObservableEmitter(io.reactivex.ObservableEmitter) TestObserver(io.reactivex.observers.TestObserver)

Example 5 with ConfigProvider

use of io.rx_cache2.ConfigProvider in project RxCache by VictorAlbertos.

the class ProxyTranslatorTest method When_Get_Page_Get_Pages.

@Test
public void When_Get_Page_Get_Pages() throws NoSuchMethodException {
    Object[] dataMethodPaginate = { Observable.just(new Object[] {}), new DynamicKey(1) };
    Method mockMethod = io.rx_cache2.internal.ProvidersRxCache.class.getDeclaredMethod("getMocksPaginate", Observable.class, DynamicKey.class);
    ConfigProvider configProvider = proxyTranslatorUT.processMethod(mockMethod, dataMethodPaginate);
    assertThat(configProvider.getDynamicKey(), is("1"));
}
Also used : ConfigProvider(io.rx_cache2.ConfigProvider) Method(java.lang.reflect.Method) EvictDynamicKey(io.rx_cache2.EvictDynamicKey) DynamicKey(io.rx_cache2.DynamicKey) Test(org.junit.Test)

Aggregations

ConfigProvider (io.rx_cache2.ConfigProvider)12 Method (java.lang.reflect.Method)9 Test (org.junit.Test)9 Observable (io.reactivex.Observable)2 DynamicKey (io.rx_cache2.DynamicKey)2 EvictDynamicKey (io.rx_cache2.EvictDynamicKey)2 EvictProvider (io.rx_cache2.EvictProvider)2 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Function (io.reactivex.functions.Function)1 TestObserver (io.reactivex.observers.TestObserver)1 Reply (io.rx_cache2.Reply)1