All Projects → kittinunf → Fuse

kittinunf / Fuse

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

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Fuse

Http Cache
High performance Golang HTTP middleware for server-side application layer caching, ideal for REST APIs
Stars: ✭ 184 (-13.21%)
Mutual labels:  cache
Golib
Go Library [DEPRECATED]
Stars: ✭ 194 (-8.49%)
Mutual labels:  cache
Immortalplayer
Free audio/video player component for Android with cache, FTP, peering, hw accel, background play, pseudo-streaming and more...
Stars: ✭ 204 (-3.77%)
Mutual labels:  cache
Simple Spring Memcached
A drop-in library to enable memcached caching in Spring beans via annotations
Stars: ✭ 185 (-12.74%)
Mutual labels:  cache
Masterchief
C# 开发辅助类库,和士官长一样身经百战且越战越勇的战争机器,能力无人能出其右。
Stars: ✭ 190 (-10.38%)
Mutual labels:  cache
Redux Cache
Client side TTL caching strategy for redux applications
Stars: ✭ 198 (-6.6%)
Mutual labels:  cache
Phpfastcache
A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
Stars: ✭ 2,171 (+924.06%)
Mutual labels:  cache
Endb
Key-value storage for multiple databases. Supports MongoDB, MySQL, Postgres, Redis, and SQLite.
Stars: ✭ 208 (-1.89%)
Mutual labels:  cache
Drone Cache
A Drone plugin for caching current workspace files between builds to reduce your build times
Stars: ✭ 194 (-8.49%)
Mutual labels:  cache
Java Library Examples
💪 example of common used libraries and frameworks, programming required, don't fork man.
Stars: ✭ 204 (-3.77%)
Mutual labels:  cache
Zbnetworking
AFNetworking4.X封装 GET/POST /PUT/PATCH /DELETE / Upload /DownLoad 网络请求 添加了请求缓存,断点下载,显示缓存大小,删除缓存,取消当前请求等功能
Stars: ✭ 186 (-12.26%)
Mutual labels:  cache
Elefant
Elefant, the refreshingly simple PHP CMS and web framework.
Stars: ✭ 188 (-11.32%)
Mutual labels:  cache
All Hies
Cached Haskell IDE Engine Nix builds for all GHC versions
Stars: ✭ 201 (-5.19%)
Mutual labels:  cache
Wechatvideocourse
《微信公众号+小程序快速开发》视频教程课件及代码
Stars: ✭ 185 (-12.74%)
Mutual labels:  cache
Laravel Partialcache
Blade directive to cache rendered partials in laravel
Stars: ✭ 205 (-3.3%)
Mutual labels:  cache
Thinkgo
A lightweight MVC framework written in Go (Golang).
Stars: ✭ 184 (-13.21%)
Mutual labels:  cache
Scaffeine
Thin Scala wrapper for Caffeine (https://github.com/ben-manes/caffeine)
Stars: ✭ 195 (-8.02%)
Mutual labels:  cache
Cachingframework.redis
Distributed caching based on StackExchange.Redis and Redis. Includes support for tagging and is cluster-compatible.
Stars: ✭ 209 (-1.42%)
Mutual labels:  cache
Redis Cache
A persistent object cache backend for WordPress powered by Redis. Supports Predis, PhpRedis, Credis, HHVM, replication and clustering.
Stars: ✭ 205 (-3.3%)
Mutual labels:  cache
Cache
📦 Nothing but Cache.
Stars: ✭ 2,491 (+1075%)
Mutual labels:  cache

Fuse

jcenter 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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].