Giter VIP home page Giter VIP logo

mycache's Introduction

为Retrofit添加两级缓存

if (!reader.useRetrofit() && !reader.useRxjava()) { return; }

以下内容需熟悉Retrofit(介绍) & Rxjava(介绍)的使用。


Retrofit使用的okhttp(介绍)已经对网络请求进行了缓存,但是如果服务器没有支持缓存该怎么办呢? 以以下接口为例:

public interface Api {
    @GET("/result.php")
    Observable<Result> getResult(@Query("value1") String value1, @Query("value2") String value2);
}

Api api = new Retrofit.Builder()
                .baseUrl(...)
                .client(new OkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
                .create(Api.class);

可能会使用这种方式来操作:

Observable<Result> network = api.getResult(...); // 从网络获取数据
Observable<Result> memory = ...; // 从内存缓存获取数据
Observable<Result> disk = ...; // 从硬盘缓存获取数据

Observable<Result> networkWithSave = network.doOnNext(data -> {
  saveToDisk(data); // 网络获取后进行缓存
  cacheInMemory(data);
});

Observable<Result> diskWithCache = disk.doOnNext(data -> {
  cacheInMemory(data); // 从硬盘缓存获取后 再缓存到内存
});

// 将上面的进行整合使用
Observable<Result> source = Observable
    .concat(memory, diskWithCache, networkWithSave)
    .filter(data -> data != null)
    .first();

蛤,由于Rxjava大法好以及lambda表达式,这种方式看起来不错,还挺简洁的。但是,如果有很多很多接口都要做缓存呢?岂不是每个都得经过一番这样的处理?

NUO! NUO! NUO! DON'T REPEAT YOURSELF!

如果能够这样实现,是不是一颗赛艇?

public interface Api {
    @GET("/result.php")
    @MyCache(timeOut = 5000)
    Observable<Result> getResult(@Query("value1") String value1, @Query("value2") String value2);
}

闷声发大财:

build.gradle

compile ('com.github.bluzwong:mycache-lib:0.1.4@aar') { transitive = true }

再换一个CallAdapterFactory

Api api = new Retrofit.Builder()
                .baseUrl(...)
                .client(new OkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                //.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用以下代替
                .addCallAdapterFactory(MyCacheRxCallAdapterFactory.create(MyCacheCore.create(context)))
                .build()
                .create(Api.class);

使用被 @MyCache(timeOut = 5000) 注解过的方法将会进行缓存


很惭愧,只做了一些微小的工作。谢谢!

mycache's People

Contributors

bluzwong avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

2017398956

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.