Requesting network data is the most frequently used function in Android development. The experience of network requests determines how users feel about the entire APP, so it is extremely important to use cache reasonably to process network requested data. Reasonable caching and network requests can bring an excellent experience to the APP. The cache of pictures includes very famous frameworks such as Picasso, Glide, and Fresco. They are extremely mature and widely used. What programmers should do is to use wheels instead of re-creating them. However, for the cache of network data, most of them are self-encapsulated, and everyone needs to perform tedious coding work. RxCache encapsulates network cache and adopts RxJava mode, which can seamlessly connect with other RxJava codes, making it extremely convenient to use.
RxCache uses LruCache and DiskLruCache to perform secondary cache of network request data, which is mainly adapted to the interface API to return data and is not used for cache of pictures, etc. You can set the cache mode, cache size, set the data expiration time, and provide the functions of deleting caches according to the key and clearing all caches. It provides Gson method and Serialize method for data storage conversion and restoration.
Project GitHub address
RxCache
Get started:
First add dependencies in the project's Gradle:
RxCache uses JitPack for dependency management, so you need to add the following code to the project first:
allprojects{ repositories{ ... maven{url ''} } }
Then add the following dependencies in the gradle of the Module:
compile ':RxCache:v1.0.0'
Initialize in your Application:
(this);//forRxCachesupplyContext
You can also use Builder for advanced initialization:
new () .setDebug(true) //Open debug, after opening, cache-related logs will be printed, default to true .setConverter(new GsonConverter()) //Set the conversion method, default is Gson conversion .setCacheMode() //Set cache mode, default to Level 2 cache .setMemoryCacheSizeByMB(50) //Set the size of the memory cache, the unit is MB .setDiskCacheSizeByMB(100) //Set the size of the disk cache, the unit is MB .setDiskDirName("RxCache") //Set the folder name of the disk cache .build();
Write to cache
() .put("test", "This is data to cache.", 10 * 1000) //key: cached key data: specific data time: cached valid time .compose(RxUtil.<Boolean>io_main()) //Thread Scheduling .subscribe(new Consumer<Boolean>() { @Override public void accept(Boolean aBoolean) throws Exception { if (aBoolean) ("Cache", "cache successful!"); } },new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { (); } });
Read cache
When reading cache, it is divided into the following situations:
If it is Gson conversion:
Read basic type data, or custom javabean data, or array data, etc. to get the data of .class
() .get("test",false,) //key: cached key update: means to force NULL to retrieve data from cache .compose(RxUtil.<CacheResponse<String>>io_main()) .subscribe(new Consumer<CacheResponse<String>>() { @Override public void accept(CacheResponse<String> stringCacheResponse) throws Exception { if(()!=null) ("data from cache : "+()); } },new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { (); } });
Read List and other data that cannot be retrieved.class. This method can also be used for the above basic data.
Type type = new TypeToken<List<String>>(){}.getType(); () .<List<String>>get("test",false,type) //Since Type is not a class, you need to specify a generic .compose(RxUtil.<CacheResponse<List<String>>>io_main()) .subscribe(new Consumer<CacheResponse<List<String>>>() { @Override public void accept(CacheResponse<List<String>> listCacheResponse) throws Exception { if(()!=null) ("data from cache : "+().toString()); } },new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { (); } });
If it is Serialize, use the following methods uniformly:
() .<List<String>>get("test",false) //Specify generics, no longer need to pass .class or Type .compose(RxUtil.<CacheResponse<List<String>>>io_main()) .subscribe(new Consumer<CacheResponse<List<String>>>() { @Override public void accept(CacheResponse<List<String>> listCacheResponse) throws Exception { if(()!=null) ("data from cache : "+().toString()); } },new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { (); } });
Clear the specified cache
() .remove("testList") .compose(RxUtil.<Boolean>io_main()) .subscribe(new Consumer<Boolean>() { @Override public void accept(Boolean aBoolean) throws Exception { if (aBoolean) ("cache data has been deleted."); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { (); } });
Clear all caches
() .clear() .compose(RxUtil.<Boolean>io_main()) .subscribe(new Consumer<Boolean>() { @Override public void accept(Boolean aBoolean) throws Exception { if (aBoolean) ("All datas has been deleted."); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { (); } });
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.