Fuse
The simple generic LRU cache for Android, backed by both memory cache ( LruCache) and disk-based cache ( DiskLruCache) by Jake Wharton
TLDR
Here is how you might wanna use Fuse to cache your network call.
Fuse.init(cacheDir.path) //jsonCache depends on Gson Fuse.jsonCache.get(URL("http://jsonplaceholder.typicode.com/users/1")) {
result ->
result.success {
json ->
//do something with json object (result is Result<JsonObject, Exception>)
}
}
//if you wanna know source of cache, can be either MEM(LruCache), DISK(DiskLruCache), NOT_FOUND(newly fetched) Fuse.stringCache.get(filesDir.resolve("json.txt")) {
result, type ->
//do something with string object (result is Result<String, Exception>)
when (type) {
Cache.Type.NOT_FOUND ->
Cache.Type.MEM ->
Cache.Type.DISK ->
}
}
Yeah, that's about it.
Installation
Dependency
Gradle
repositories {
jcenter()
}
dependencies {
compile 'com.github.kittinunf.fuse:fuse:<latest-version>'
}
Detail Usage
Built-in Cache
By default, Fuse
has built-in Byte, String and JSONObject cache by using Fuse.byteCache
, Fuse.stringCache
and Fuse.jsonCache
respectively
Remove
You can remove specific cache by using Key
Fuse.bytesCache.remove("key") //same for stringCache and jsonCache
Advanced Usage
How it works?
- Fuse searches at 1st layer at LruCache (Memory), if found, delivers. If not found go to 2.
- Fuse searches at 2nd layer at DiskLruCache (Disk), if found delivers, If not found go to 3.
- Fuse performs fetch (by conformance with Fetcher interface), then store into LruCache and DiskCache, respectively. Therefore, subsequent uses will be much faster by going through 1 & 2.
License
Fuse is released under MIT, but as Fuse depends on LruCache and DiskLruCache. Licenses agreement on both dependencies applies.
Copyright 2011 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.