4689ca59c7
* [Temp] Remove all modules * Remove references to Store 4 * Update CHANGELOG * Update README * Add Store 5 foundation Signed-off-by: mramotar <mramotar@dropbox.com> * Dump API Signed-off-by: mramotar <mramotar@dropbox.com> * Delete scratch API Signed-off-by: mramotar <mramotar@dropbox.com> * Fix workflow Signed-off-by: mramotar <mramotar@dropbox.com> * Rename clear to delete Signed-off-by: mramotar <mramotar@dropbox.com> * Flatten sealed classes Signed-off-by: mramotar <mramotar@dropbox.com> * Make ShareableLruCache threadsafe Signed-off-by: mramotar <mramotar@dropbox.com> * First stab Signed-off-by: mramotar <mramotar@dropbox.com> * Remove scope Signed-off-by: mramotar <mramotar@dropbox.com> * Remove scope Signed-off-by: mramotar <mramotar@dropbox.com> * Cherry pick Signed-off-by: mramotar <mramotar@dropbox.com> * Remove scope Signed-off-by: mramotar <mramotar@dropbox.com> * Fix multithreading LRU cache test Signed-off-by: mramotar <mramotar@dropbox.com> * Remove logs Signed-off-by: mramotar <mramotar@dropbox.com> * Cover multithreaded market write Signed-off-by: mramotar <mramotar@dropbox.com> * Remove redundant advance Signed-off-by: mramotar <mramotar@dropbox.com> * Remove Stately !! Signed-off-by: mramotar <mramotar@dropbox.com> * Use mutex Signed-off-by: mramotar <mramotar@dropbox.com> * Refactor Store to interface with factory Signed-off-by: mramotar <mramotar@dropbox.com> * Rename to MemoryLruCache Signed-off-by: mramotar <mramotar@dropbox.com> * Rename to RealMarket Signed-off-by: mramotar <mramotar@dropbox.com> * Rename to delete all Signed-off-by: mramotar <mramotar@dropbox.com> * Rename tests Signed-off-by: mramotar <mramotar@dropbox.com> * Clean up Android LRU cache tests Signed-off-by: mramotar <mramotar@dropbox.com> * Fix market factory Signed-off-by: mramotar <mramotar@dropbox.com> * Rename ConflictResolver to Bookkeeper Signed-off-by: mramotar <mramotar@dropbox.com> * From desktop Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Fix tests Signed-off-by: mramotar <mramotar@dropbox.com> * Document Lightswitch Signed-off-by: mramotar <mramotar@dropbox.com> * Guard against multiple releases Signed-off-by: mramotar <mramotar@dropbox.com> * Rename to StoreSafety Signed-off-by: mramotar <mramotar@dropbox.com> * Use withLock Signed-off-by: mramotar <mramotar@dropbox.com> * Document load Signed-off-by: mramotar <mramotar@dropbox.com> * Make delete and deleteAll optional Signed-off-by: mramotar <mramotar@dropbox.com> * Add foundation for sample app Signed-off-by: mramotar <mramotar@dropbox.com> * Remove Kotlin serialization Signed-off-by: mramotar <mramotar@dropbox.com> * Rename masterLock to mainLock Signed-off-by: mramotar <mramotar@dropbox.com> * Rename Bookkeeper methods Signed-off-by: mramotar <mramotar@dropbox.com> * Rename using Market/Network prefix Signed-off-by: mramotar <mramotar@dropbox.com> * Make NetworkFetcher an interface Signed-off-by: mramotar <mramotar@dropbox.com> * Make NetworkUpdater an interface Signed-off-by: mramotar <mramotar@dropbox.com> * Make MarketReader an interface Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Make MarketWriter an interface Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add app and main activity Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add scoping Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Remove unused mipmap Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add Dagger components Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Remove icon Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Draft README.md Signed-off-by: mramotar <mramotar@dropbox.com> * Wire account tab Signed-off-by: mramotar <mramotar@dropbox.com> * Remove campaigns sample Signed-off-by: mramotar <mramotar@dropbox.com> * Add Market wiring Signed-off-by: mramotar <mramotar@dropbox.com> * Remove unused resources Signed-off-by: mramotar <mramotar@dropbox.com> * Fix Gradle plugins Signed-off-by: mramotar <mramotar@dropbox.com> * Fix rebase issue Signed-off-by: mramotar <mramotar@dropbox.com> * Fix wiring Signed-off-by: mramotar <mramotar@dropbox.com> * Introduce GoodValidator Signed-off-by: mramotar <mramotar@dropbox.com> * Add unit tests Signed-off-by: mramotar <mramotar@dropbox.com> * Only load if read not in progress Signed-off-by: mramotar <mramotar@dropbox.com> * Update CI and autogenerate Swift Package (#469) * Update CI and autogenerate Swift Package Signed-off-by: mramotar <mramotar@dropbox.com> * Update CI and autogenerate Swift Package Signed-off-by: mramotar <mramotar@dropbox.com> * Run on MacOS Signed-off-by: mramotar <mramotar@dropbox.com> * Only run on push Signed-off-by: mramotar <mramotar@dropbox.com> Signed-off-by: mramotar <mramotar@dropbox.com> * Use KMM Bridge (#470) * Clean up deps Signed-off-by: mramotar <mramotar@dropbox.com> * Use KMM Bridge Signed-off-by: mramotar <mramotar@dropbox.com> * Fix version prefix Signed-off-by: mramotar <mramotar@dropbox.com> Signed-off-by: mramotar <mramotar@dropbox.com> * Fix Maven publish plugin (#471) Signed-off-by: mramotar <mramotar@dropbox.com> Signed-off-by: mramotar <mramotar@dropbox.com> * Refactor fetcher and updater to 1:1 with market Signed-off-by: mramotar <mramotar@dropbox.com> * Reset Fake API Signed-off-by: mramotar <mramotar@dropbox.com> * Remove unused imports Signed-off-by: mramotar <mramotar@dropbox.com> * Run check on store Signed-off-by: mramotar <mramotar@dropbox.com> * Format Signed-off-by: mramotar <mramotar@dropbox.com> * Api Dump + Format Signed-off-by: mramotar <mramotar@dropbox.com> * Add default config Signed-off-by: mramotar <mramotar@dropbox.com> * Check all Signed-off-by: mramotar <mramotar@dropbox.com> * Add ItemValidator Signed-off-by: mramotar <mramotar@dropbox.com> * README.md Signed-off-by: mramotar <mramotar@dropbox.com> * With ViewModel Signed-off-by: mramotar <mramotar@dropbox.com> * Format Signed-off-by: mramotar <mramotar@dropbox.com> * Implement fetcher and updater Signed-off-by: mramotar <mramotar@dropbox.com> * Cleanup Readme/Remove Persister (#474) * cleanup readme and rename a few concepts * fix tests * readme cleanup * readme cleanup * readme cleanup * readme cleanup * lint Co-authored-by: Mike Nakhimovich <mnakhimovich@Mikes-MBP.localdomain> * Regenerate gradle-wrapper.jar Signed-off-by: mramotar <mramotar@dropbox.com> * Prepare for release 5.0.0-alpha1 Signed-off-by: mramotar <mramotar@dropbox.com> * Prepare for next development version Signed-off-by: mramotar <mramotar@dropbox.com> * Target browser and Node! Signed-off-by: mramotar <mramotar@dropbox.com> * Only generate Swift Package on release Signed-off-by: mramotar <mramotar@dropbox.com> * Fix iOS targets Signed-off-by: mramotar <mramotar@dropbox.com> * Fix workflow Signed-off-by: mramotar <mramotar@dropbox.com> * Fix workflow Signed-off-by: mramotar <mramotar@dropbox.com> * Run manually only Signed-off-by: mramotar <mramotar@dropbox.com> * Update RELEASING.md Signed-off-by: mramotar <mramotar@dropbox.com> * Update README.md Signed-off-by: matt-ramotar <mramotar@dropbox.com> * Run on macos Signed-off-by: mramotar <mramotar@dropbox.com> * Remove Dropbox references Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Prepare for release 5.0.0-alpha02 Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Prepare next development version Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Update CHANGELOG.md Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add tests for complex markets Signed-off-by: mramotar <mramotar@dropbox.com> * Fix import Signed-off-by: mramotar <mramotar@dropbox.com> * Fix tests Signed-off-by: mramotar <mramotar@dropbox.com> * Format Signed-off-by: mramotar <mramotar@dropbox.com> * Distinguish network, store, and common representations Signed-off-by: mramotar <mramotar@dropbox.com> * Update NetworkFetcher Signed-off-by: mramotar <mramotar@dropbox.com> * Refactor inputs + outputs Signed-off-by: mramotar <mramotar@dropbox.com> * Update sample + fix tests Signed-off-by: mramotar <mramotar@dropbox.com> * Update comment Signed-off-by: mramotar <mramotar@dropbox.com> * Port Cache Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Update README.md Signed-off-by: Matt <mramotar@dropbox.com> * Move README.md Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Update README.md Signed-off-by: Matt <mramotar@dropbox.com> * Update README.md Signed-off-by: Matt <mramotar@dropbox.com> * Merge multicast from github.com/MobileNativeFoundation/Store/tree/Kmp Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Fix build errors Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Format Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Format Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Move to market package Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add Fetcher + FetcherResult Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Api dump + format Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Port Store Signed-off-by: mramotar <mramotar@dropbox.com> * Add unit test Signed-off-by: mramotar <mramotar@dropbox.com> * Format Signed-off-by: mramotar <mramotar@dropbox.com> * Fix tests Signed-off-by: mramotar <mramotar@dropbox.com> * Update .gitignore Signed-off-by: mramotar <mramotar@dropbox.com> * Api Dump Signed-off-by: mramotar <mramotar@dropbox.com> * Cover stream and fresh Signed-off-by: mramotar <mramotar@dropbox.com> * Fix tests Signed-off-by: mramotar <mramotar@dropbox.com> * Run on MacOS Signed-off-by: mramotar <mramotar@dropbox.com> * Use Java 11 Signed-off-by: mramotar <mramotar@dropbox.com> * Add HotFlowStoreTest Signed-off-by: mramotar <mramotar@dropbox.com> * Add gradle plugin repository Signed-off-by: mramotar <mramotar@dropbox.com> * Update check.yml Signed-off-by: Matt <mramotar@dropbox.com> * Update check.yml Signed-off-by: Matt <mramotar@dropbox.com> * Add ClearAllStoreTests Signed-off-by: mramotar <mramotar@dropbox.com> * Use Java 11 Signed-off-by: mramotar <mramotar@dropbox.com> * Format Signed-off-by: mramotar <mramotar@dropbox.com> * Fix tests Signed-off-by: mramotar <mramotar@dropbox.com> * Remove binary-compatibility-validator Signed-off-by: mramotar <mramotar@dropbox.com> * Ignore Store podspec Signed-off-by: mramotar <mramotar@dropbox.com> * Update check.yml Signed-off-by: mramotar <mramotar@dropbox.com> * Use Java 11 Signed-off-by: mramotar <mramotar@dropbox.com> * Use channel flow Signed-off-by: mramotar <mramotar@dropbox.com> * Format Signed-off-by: mramotar <mramotar@dropbox.com> * Fix tests Signed-off-by: mramotar <mramotar@dropbox.com> * Revert workflow Signed-off-by: mramotar <mramotar@dropbox.com> * Disable transformation Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Disable transformation Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Disable transformation Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add ClearStoreByKeyTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add FetcherResponseTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add KeyTrackerTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add StoreWithInMemoryCacheTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add StreamWithoutSourceOfTruthTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * ValueFetcherTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add MapIndexedTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add FetcherControllerTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add StoreResponseTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add SourceOfTruthErrorsTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Add SourceOfTruthWithBarrierTests Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Remove Market and update README Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Remove Market sample Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Update README Signed-off-by: Matt Ramotar <mramotar@dropbox.com> * Update CHANGELOG Signed-off-by: Matt Ramotar <mramotar@dropbox.com> Signed-off-by: mramotar <mramotar@dropbox.com> Signed-off-by: Matt Ramotar <mramotar@dropbox.com> Signed-off-by: matt-ramotar <mramotar@dropbox.com> Signed-off-by: Matt <mramotar@dropbox.com> Co-authored-by: Mike Nakhimovich <digitalbuddha@users.noreply.github.com> Co-authored-by: Mike Nakhimovich <mnakhimovich@Mikes-MBP.localdomain> |
||
---|---|---|
.github | ||
buildSrc | ||
cache | ||
gradle | ||
Images | ||
multicast | ||
store | ||
.codecov.yml | ||
.gitignore | ||
build.gradle.kts | ||
CHANGELOG.md | ||
CODE_OF_CONDUCT.md | ||
CONTRIBUTING.md | ||
gradle.properties | ||
gradlew | ||
gradlew.bat | ||
LICENSE | ||
pull_request_template.md | ||
README.md | ||
RELEASING.md | ||
settings.gradle |
Store 5
Why We Made Store
- Modern software needs data representations to be fluid and always available.
- Users expect their UI experience to never be compromised (blocked) by new data loads. Whether an application is social, news or business-to-business, users expect a seamless experience both online and offline.
- International users expect minimal data downloads as many megabytes of downloaded data can quickly result in astronomical phone bills.
Store is a Kotlin library for loading data from remote and local sources.
Overview
A Store is responsible for managing a particular data request. When you create an implementation of
a Store, you provide it with a Fetcher
, a function that defines how data will be fetched over
network. You can also define how your Store will cache data in-memory and on-disk. Since Store
returns your data as a Flow
, threading is a breeze! Once a Store is built, it handles the logic
around data flow, allowing your views to use the best data source and ensuring that the newest data
is always available for later offline use.
Store leverages multiple request throttling to prevent excessive calls to the network and disk cache. By utilizing Store, you eliminate the possibility of flooding your network with the same request while adding two layers of caching (memory and disk) as well as ability to add disk as a source of truth where you can modify the disk directly without going through Store (works best with databases that can provide observables sources like Jetpack Room , SQLDelight or Realm)
How to include in your project
Artifacts are hosted on Maven Central.
STORE_VERSION = "5.0.0-alpha03"
Android
implementation "org.mobilenativefoundation.store:store5:$STORE_VERSION"
Multiplatform (Common, JVM, Native, JS)
commonMain {
dependencies {
implementation("org.mobilenativefoundation.store:store5:$STORE_VERSION")
}
}
Fully Configured Store
Let's start by looking at what a fully configured Store looks like. We will then walk through simpler examples showing each piece:
StoreBuilder
.from(
fetcher = Fetcher.of { api.fetchSubreddit(it, "10").data.children.map(::toPosts) },
sourceOfTruth = SourceOfTruth.of(
reader = db.postDao()::loadPosts,
writer = db.postDao()::insertPosts,
delete = db.postDao()::clearFeed,
deleteAll = db.postDao()::clearAllFeeds
)
).build()
With the above setup you have:
- In-memory caching for rotation
- Disk caching for when users are offline
- Throttling of API calls when parallel requests are made for the same resource
- Rich API to ask for data whether you want cached, new or a stream of future data updates.
And now for the details:
Creating a Store
You create a Store using a builder. The only requirement is to include a Fetcher
which is just
a typealias
to a function that returns a Flow<FetcherResult<ReturnType>>
.
val store = StoreBuilder
.from(Fetcher.ofFlow { articleId -> api.getArticle(articleId) }) // api returns Flow<Article>
.build()
Store uses generic keys as identifiers for data. A key can be any value object that properly
implements toString()
, equals()
and hashCode()
. When your Fetcher
function is called, it
will be passed a particular Key
value. Similarly, the key will be used as a primary identifier
within caches (Make sure to have a proper hashCode()
!!).
Note: We highly recommend using built-in types that implement equals
and hashcode
or
Kotlin data
classes for complex keys.
Public Interface - Stream
The primary function provided by a Store
instance is the stream
function which has the following
signature:
fun stream(request: StoreRequest<Key>): Flow<StoreResponse<Output>>
Each stream
call receives a StoreRequest
object, which defines which key to fetch and which data
sources to utilize.
The response is a Flow
of StoreResponse
. StoreResponse
is a Kotlin sealed class that can be
either
a Loading
, Data
or Error
instance.
Each StoreResponse
includes an origin
field which specifies where the event is coming from.
- The
Loading
class only has anorigin
field. This can provide you information like "network is fetching data", which can be a good signal to activate the loading spinner in your UI. - The
Data
class has avalue
field which includes an instance of the type returned byStore
. - The
Error
class includes anerror
field that contains the exception thrown by the givenorigin
.
When an error happens, Store
does not throw an exception, instead, it wraps it in
a StoreResponse.Error
type which allows Flow
to continue so that it can still receive updates
that might be triggered by either changes in your data source or subsequent fetch operations.
viewModelScope.launch {
store.stream(StoreRequest.cached(key = key, refresh = true)).collect { response ->
when (response) {
is StoreResponse.Loading -> showLoadingSpinner()
is StoreResponse.Data -> {
if (response.origin == ResponseOrigin.Fetcher) hideLoadingSpinner()
updateUI(response.value)
}
is StoreResponse.Error -> {
if (response.origin == ResponseOrigin.Fetcher) hideLoadingSpinner()
showError(response.error)
}
}
}
}
For convenience, there are Store.get(key)
and Store.fresh(key)
extension functions.
suspend fun Store.get(key: Key): Value
: This method returns a single value for the given key. If available, it will be returned from the in memory cache or the sourceOfTruth. An error will be thrown if no value is available in either thecache
orsourceOfTruth
, and thefetcher
fails to load the data from the network.suspend fun Store.fresh(key: Key): Value
: This method returns a single value for the given key that is obtained by querying the fetcher. An error will be thrown if thefetcher
fails to load the data from the network, regardless of whether any value is available in thecache
orsourceOfTruth
.
lifecycleScope.launchWhenStarted {
val article = store.get(key)
updateUI(article)
}
The first time you call to suspend store.get(key)
, the response will be stored in an in-memory
cache and in the sourceOfTruth, if provided.
All subsequent calls to store.get(key)
with the same Key
will retrieve the cached version of the
data, minimizing unnecessary data calls. This prevents your app from fetching fresh data over the
network (or from another external data source) in situations when doing so would unnecessarily waste
bandwidth and battery. A great use case is any time your views are recreated after a rotation, they
will be able to request the cached data from your Store. Having this data available can help you
avoid the need to retain this in the view layer.
By default, 100 items will be cached in memory for 24 hours. You may pass in your own memory policy to override the default policy.
Skipping Memory/Disk
Alternatively, you can call store.fresh(key)
to get a suspended result
that skips the memory (
and optional disk cache).
A good use case is overnight background updates use fresh()
to make sure that calls
to store.get()
will not have to hit the network during normal usage. Another good use case
for fresh()
is when a user wants to pull to refresh.
Calls to both fresh()
and get()
emit one value or throw an error.
Stream
For real-time updates, you may also call store.stream()
which returns a Flow<T>
that emits each
time a new item is returned from your store. You can think of stream as a way to create reactive
streams that update when you db or memory cache updates
example calls:
lifecycleScope.launchWhenStarted {
store.stream(
StoreRequest.cached(
3,
refresh = false
)
) //will get cached value followed by any fresh values, refresh will also trigger network call if set to `true` even if the data is available in cache or disk.
.collect {}
store.stream(StoreRequest.fresh(3)) //skip cache, go directly to fetcher
.collect {}
}
Inflight Debouncer
To prevent duplicate requests for the same data, Store offers an inflight debouncer. If the same
request is made as a previous identical request that has not completed, the same response will be
returned. This is useful for situations when your app needs to make many async calls for the same
data at startup or when users are obsessively pulling to refresh. As an example, The New York Times
news app asynchronously calls ConfigStore.get()
from 12 different places on startup. The first
call blocks while all others wait for the data to arrive. We have seen a dramatic decrease in the
app's data usage after implementing this inflight logic.
Disk as Cache
Stores can enable disk caching by passing a SourceOfTruth
into the builder. Whenever a new network
request is made, the Store will first write to the disk cache and then read from the disk cache.
Disk as Single Source of Truth
Providing sourceOfTruth
whose reader
function can return a Flow<Value?>
allows you to make
Store treat your disk as source of truth.
Any changes made on disk, even if it is not made by Store, will update the active Store
streams.
This feature, combined with persistence libraries that provide observable queries (Jetpack Room , SQLDelight or Realm) allows you to create offline first applications that can be used without an active network connection while still providing a great user experience.
StoreBuilder
.from(
fetcher = Fetcher.of { api.fetchSubreddit(it, "10").data.children.map(::toPosts) },
sourceOfTruth = SourceOfTruth.of(
reader = db.postDao()::loadPosts,
writer = db.postDao()::insertPosts,
delete = db.postDao()::clearFeed,
deleteAll = db.postDao()::clearAllFeeds
)
).build()
Stores don’t care how you’re storing or retrieving your data from disk. As a result, you can use Stores with object storage or any database (Realm, SQLite, CouchDB, Firebase etc). Technically, there is nothing stopping you from implementing an in-memory cache for the "sourceOfTruth" implementation and instead have two levels of in-memory caching--one with inflated and one with deflated models, allowing for sharing of the “sourceOfTruth” cache data between stores.
If using SQLite we recommend working
with Room which returns a Flow
from a query
The above builder is how we recommend working with data on Android. With the above setup you have:
- Memory caching with TTL & Size policies
- Disk caching with simple integration with Room
- In-flight request management
- Ability to get cached data or bust through your caches (
get()
vs.fresh()
) - Ability to listen for any new emissions from network (stream)
- Structured Concurrency through APIs build on Coroutines and Kotlin Flow
Configuring in-memory Cache
You can configure in-memory cache with the MemoryPolicy
:
StoreBuilder
.from(
fetcher = Fetcher.of { api.fetchSubreddit(it, "10").data.children.map(::toPosts) },
sourceOfTruth = SourceOfTruth.of(
reader = db.postDao()::loadPosts,
writer = db.postDao()::insertPosts,
delete = db.postDao()::clearFeed,
deleteAll = db.postDao()::clearAllFeeds
)
).cachePolicy(
MemoryPolicy.builder<Any, Any>()
.setMaxSize(10)
.setExpireAfterAccess(10.minutes) // or setExpireAfterWrite(10.minutes)
.build()
).build()
setMaxSize(maxSize: Long)
sets the maximum number of entries to be kept in the cache before starting to evict the least recently used items.setExpireAfterAccess(expireAfterAccess: Duration)
sets the maximum time an entry can live in the cache since the last access, where "access" means reading the cache, adding a new cache entry, and replacing an existing entry with a new one. This duration is also known as time-to-idle (TTI).setExpireAfterWrite(expireAfterWrite: Duration)
sets the maximum time an entry can live in the cache since the last write, where "write" means adding a new cache entry and replacing an existing entry with a new one. This duration is also known as time-to-live (TTL).
Note that setExpireAfterAccess
and setExpireAfterWrite
cannot both be set at the same time.
Clearing store entries
You can delete a specific entry by key from a store, or clear all entries in a store.
Store with no sourceOfTruth
val store = StoreBuilder
.from(
fetcher = Fetcher.of { key: String ->
api.fetchData(key)
}).build()
The following will clear the entry associated with the key from the in-memory cache:
store.clear("10")
The following will clear all entries from the in-memory cache:
store.clearAll()
Store with sourceOfTruth
When store has a sourceOfTruth, you'll need to provide the delete
and deleteAll
functions
for clear(key)
and clearAll()
to work:
StoreBuilder
.from(
fetcher = Fetcher.of { api.fetchData(key) },
sourceOfTruth = SourceOfTruth.of(
reader = dao::loadData,
writer = dao::writeData,
delete = dao::clearDataByKey,
deleteAll = dao::clearAllData
)
).build()
The following will clear the entry associated with the key from both the in-memory cache and the sourceOfTruth:
store.clear("myKey")
The following will clear all entries from both the in-memory cache and the sourceOfTruth:
store.clearAll()
License
Copyright (c) 2022 Mobile Native Foundation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.