Code Monkey home page Code Monkey logo

Comments (8)

z-chu avatar z-chu commented on June 6, 2024

firstCache 只会发出一个结果,也就是调用onNext()一次,优先取缓存中的如果缓存中有数据,就不会在取网络了
cacheAndRemote 如果有缓存会发出2个结果,也就是有缓存就会调用onNext()2次,先发出缓存的的结果,在发出网络的结果,如果没有缓存就只会有网络的结果发出

from rxcache.

GGjin avatar GGjin commented on June 6, 2024

你好 那我想 优先显示缓存,同时请求网络,网络有数据的时候 刷新缓存,显示网络数据,网络数据请求失败的时候,或者无网的时候,就显示缓存的数据,可以搞嘛?

from rxcache.

z-chu avatar z-chu commented on June 6, 2024

你这个需求cacheAndRemote 应该是可以实现的才对

from rxcache.

z-chu avatar z-chu commented on June 6, 2024

你可以看下源码,重写IObservableStrategy或者IFlowableStrategy 接口定制你的缓存策略

from rxcache.

GGjin avatar GGjin commented on June 6, 2024

好的 谢谢啦

from rxcache.

GGjin avatar GGjin commented on June 6, 2024

cacheAndRemote这个缓存策略当无网的时候 也还是会请求网络,最后显示不出来数据。。。

from rxcache.

z-chu avatar z-chu commented on June 6, 2024

会发出2次结果,如果网络失败了第二次是会走onError的,如果你不想第二次的结果走onError,可以在CacheAndRemoteStrategy 的基础上小作修改
这样:

public  class CustomStrategy implements IStrategy {
    private boolean isSync;

    public CacheAndRemoteStrategy() {
        isSync = false;
    }

    public CacheAndRemoteStrategy(boolean isSync) {
        this.isSync = isSync;
    }

    @Override
    public <T> Observable<CacheResult<T>> execute(RxCache rxCache, String key, Observable<T> source, Type type) {
        Observable<CacheResult<T>> cache = RxCacheHelper.loadCache(rxCache, key, type, true);
        Observable<CacheResult<T>> remote;
        if (isSync) {
            remote = RxCacheHelper.loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk, true);//把最后一个参数改为true
        } else {
            remote = RxCacheHelper.loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk, true);//把最后一个参数改为true
        }
        return Observable.concat(cache, remote)
                .filter(new Predicate<CacheResult<T>>() {
                    @Override
                    public boolean test(@NonNull CacheResult<T> result) throws Exception {
                        return result != null && result.getData() != null;
                    }
                });
    }

    @Override
    public <T> Publisher<CacheResult<T>> flow(RxCache rxCache, String key, Flowable<T> source, Type type) {
        Flowable<CacheResult<T>> cache = RxCacheHelper.loadCacheFlowable(rxCache, key, type, true);
        Flowable<CacheResult<T>> remote;
        if (isSync) {
            remote = RxCacheHelper.loadRemoteSyncFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk, true);//把最后一个参数改为true
        } else {
            remote = RxCacheHelper.loadRemoteFlowable(rxCache, key, source, CacheTarget.MemoryAndDisk, true);//把最后一个参数改为true
        }
        return Flowable.concat(cache, remote)
                .filter(new Predicate<CacheResult<T>>() {
                    @Override
                    public boolean test(@NonNull CacheResult<T> result) throws Exception {
                        return result != null && result.getData() != null;
                    }
                });
    }
}

和已有的缓存策略使用方式是一样的:

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

这样如果网络请求失败了使不会再走onError的

from rxcache.

GGjin avatar GGjin commented on June 6, 2024

好的 我试试 麻烦了大佬

from rxcache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.