Coder Social home page Coder Social logo

kittinunf / fuse Goto Github PK

View Code? Open in Web Editor NEW
275.0 11.0 26.0 5.57 MB

The simple generic LRU memory/disk cache for Android written in Kotlin

Home Page: https://kittinunf.gitbook.io/fuse/

Kotlin 100.00%
kotlin android cache lru-cache disklrucache

fuse's Introduction

Fuse

MavenCentral Build Status Codecov

The simple generic LRU cache for Android, backed by both memory cache (LruCache) and disk-based cache (DiskLruCache) by Jake Wharton

Installation

The core package has following dependencies;

  //core
  implementation 'com.github.kittinunf.fuse:fuse:<latest-version>'
  
  //android
  implementation 'com.github.kittinunf.fuse:fuse-android:<latest-version>'

How to use

Fuse is designed to be simple and easy to use. All you need is CacheBuilder to setup configuration for your cache.

private val tempDir = createTempDir().absolutePath // use any readable/writable directory of your choice

val convertible = // there are couple of built-in Convertibles such as DataConvertible, StringDataConvertible
val cache = CacheBuilder.config(tempDir, convertible) { 
  // do more configuration here
}.build()

Then, you can build Cache from the CacheBuilder and, you can start using your cache like;

cache = //cache instance that was instantiated earlier

//put value "world" for key "hello", "put" will always add new value into the cache
cache.put("hello", { "world" })

//later
cache.get("hello") // this returns Result.Success["world"]
val (result, source) = cache.getWithSource("hello") // this also returns Source which is one of the following, 1. MEM, 2. DISK, 3. ORIGIN


val result = cache.get("hello", { "world" }) // this return previously cached value otherwise it will save value "world" into the cache for later use
when (result) {
  is Success -> { // value is successfully return/fetch, result.value is data 
  }
  is Failure -> { // something wrong, check result.error for more details 
  }
}

Source

Source gives you an information where the data is coming from.

enum class Source {
  ORIGIN,
  DISK,
  MEM
}
  • ORIGIN - The data is coming from the original source. This means that it is being fetched from the Fetcher<T> class.
  • DISK - The data is coming from the Disk cache. In this cache, it is specifically retrieved from DiskLruCache
  • MEM - The data is coming from the memory cache.

All of the interfaces that provides Source have WithSource suffix, i.e. getWithSource() etc.

Android Usage

For Android, it is basically a thin layer on top of the memory cache by using a LruCache

// same configuration as above
val cache = CacheBuilder.config(tempDir, convertible) {
  // do more configuration here
  memCache = defaultAndroidMemoryCache() // this will utilize the LruCache provided by Android SDK
}.build()

By default, the Cache is perpetual meaning that it will never expired by itself. Please check [Detail Usage] for more information about expirable cache.

Detailed usage

The default Cache that is provided by Fuse is a perpetual cache that will never expire the entry. In some cases, this is not what you want. If you are looking for non-perpetual cache, luckily, Fuse also provides you with a ExpirableCache as well.

The usage of ExpirableCache is almost exactly the same as a regular cache, but with a time constraint that is configurable for the entry to be expired.

private val cache = CacheBuilder.config(tempDir, StringDataConvertible()).build().let(::ExpirableCache)

// cache is ExpirableCache type
val (value, error) = expirableCache.get("hello", { "world" }) // this works the same as regular cache

println(value) //Result.Success["world"]

// after 5 seconds has passed
val (value, error) = expirableCache.get("hello", { "new world" }, timeLimit = 5.seconds) // if the cached value has a lifetime longer than 5 seconds, entry becomes invalid

println(value) //Result.Success["new world"], it got refreshed as the entry is expired

Sample

Please see the sample Android app that utilize Fuse in the Sample folder

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.

fuse's People

Contributors

babedev avatar eschlenz avatar kittinunf avatar zalewskise avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fuse's Issues

UnsupportedOperationException

val expiringCache = ExpirableCache(cache)
expiringCache.getWithSource(DeviceDetailsFetcher(), timeLimit = CACHE_PERIOD)

java.lang.UnsupportedOperationException: This function has a reified type parameter and thus can only be inlined at compilation time, not called directly.
at kotlin.jvm.internal.Intrinsics.throwUndefinedForReified(Intrinsics.java:207)
at kotlin.jvm.internal.Intrinsics.throwUndefinedForReified(Intrinsics.java:201)
at kotlin.jvm.internal.Intrinsics.reifiedOperationMarker(Intrinsics.java:211)
at com.github.kittinunf.result.Result$Companion.of(Result.kt:244)
at com.github.kittinunf.fuse.core.CacheImpl._get(Cache.kt:74)
at com.github.kittinunf.fuse.core.CacheImpl.getWithSource(Cache.kt:63)
at com.github.kittinunf.fuse.core.scenario.ExpirableCache.getWithSource-8Mi8wO0(ExpirableCache.kt:67)
at com.github.kittinunf.fuse.core.scenario.ExpirableCache.getWithSource-8Mi8wO0$default(ExpirableCache.kt:51)

classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

implementation 'com.github.kittinunf.fuse:fuse-android:1.2.2'

Mem cache reads (hits) should be fast, but are slowed down by disk cache updates.

Hi,

First of all, thanks for open sourcing this project. It's really cool, and easy to use. ๐Ÿ‘

In using this library I have found something that, I believe, is problematic. My expectation is that a mem cache hit should return very quickly. The data is readily available, and nothing else should hold that up.

I believe, however, that even if data is readily available from memory, the library is still doing disk operations before returning this data. It appears to be fully reading the disk cache entry, and then writing it back out in order to bump it up in the LRU cache. https://github.com/kittinunf/Fuse/blob/master/fuse/src/main/java/com/github/kittinunf/fuse/core/Cache.kt#L66-L75

This reading and writing of the entry drastically slows down what should have been a fast operation. And in a situation (like the one I'm in), is extremely noticeable when performing hundreds of cache reads in a short period of time.

I believe what you are trying to do (bump the disk LRU cache entry up) makes sense. I'm just wondering if there is a way that can be done asynchronously, so as not to hold up returning the data that was available from memory.

Thoughts? I'd be happy to collaborate with you on this.

Removing from Cache not working as intended?

Trying to remove a Website from the Cache using Fuse.stringCache.remove(url)
url is the String of the Website I used to download.

However when using it like so I get the following Exception:

 java.lang.IllegalArgumentException: keys must match regex [a-z0-9_-]{1,64}: "https://www.wired.com/2017/11/gifts-for-the-apple-iphone-lover/"
                                                                             at com.jakewharton.disklrucache.DiskLruCache.validateKey(DiskLruCache.java:660)
                                                                             at com.jakewharton.disklrucache.DiskLruCache.remove(DiskLruCache.java:581)
                                                                             at com.github.kittinunf.fuse.core.cache.DiskCache.remove(DiskCache.kt:40)
                                                                             at com.github.kittinunf.fuse.core.Cache.remove(Cache.kt:113)
                                                                             at com.github.kittinunf.fuse.core.Cache.remove$default(Cache.kt:110)

What I tried:

  1. Matching the url to the Regex - did not work as it is not the key
  2. Calling it using Fuse.stringCache.remove(URL(url).toString()) also not finding the Item to remove

What worked was Fuse.stringCache.remove(url.md5()). Is that the way it is supposed to work?
Edit: This did not remove the Value from the Cache

Maybe adding a function to remove Items by URL similar to the get function would be a consideration?

Exposing Put

Is there a reason why put isn't exposed like in any regular caching library?

There're certain use cases where you want to update the cache without getting its content, plus it isn't very convenient when used in conjunction with RxJava given its async nature.

Check for Cached Item

Is there a way to check if an Item with a given Key is currently cached? Or in other words don't perform a network call when it is not cached?

Memory cache sizing

Nice job on the library!

When looking at MemCache.kt, line 9https://github.com/kittinunf/Fuse/blob/master/fuse/src/main/kotlin/com/github/kittinunf/fuse/core/cache/MemCache.kt#L9, I can see that you're setting the size of the cache to 1/8th of the avialable memory, as is described here: https://developer.android.com/topic/performance/graphics/cache-bitmap.html#memory-cache

However, you're not overriding sizeOf, so I believe you'll accept a number of items equal to that value, rather than the number of kilobytes.

Or did I miss something?

Is there a LruCache bug ?

Hi !
In the Android example the size of Lru cache is set in bytes but the sizeOf method has not been overriden. In this way the LRU size method return the number of element in the LRU cache and not the size in byte.

integration with Fuel

How can i use this with Fuel. as i want local cache inside my app which have a lot of network calls?

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.