Store5 Documentation

repository·main·Indexed 25 days ago

https://github.com/mobilenativefoundation/store

A library for managing data loading and caching in mobile applications, providing a structured approach to data fetching and persistence. It includes features such as StoreMultiCache for multi-layer caching of single items and collections, a Multicaster for broadcasting data to multiple subscribers, and a Converter system for transforming data between network, local, and output models. The library also provides configurable MemoryPolicy for in-memory caching via size or weight-based limits and expiration policies.

Tokens
3.1K
Snippets
3
Records
20
Agent score
86%

What's inside Store5

  1. Get started with Store5

    main

    To begin using Store5, follow these recommended steps:

    1. Quickstart: Follow the Quickstart guide to build your first Store instance.
    2. Learn Core Concepts: Read the Store Foundations documentation to understand the underlying architecture and how Store works.
    3. Advanced Operations: For complex data management, refer to the guide on Handling CRUD to learn how to support create, read, update, and delete operations.
  2. Apply the AndroidConventionPlugin to your project

    main

    The AndroidConventionPlugin is a Gradle plugin that automates the setup of Android library projects. When applied, it automatically configures the following plugins:

    • com.android.library (Android Library)
    • com.vanniktech.maven.publish (Maven Publishing)
    • org.jetbrains.dokka (Documentation generation)
    • maven-publish (Standard Maven publishing)
    • org.jetbrains.kotlinx.binary-compatibility-validator (API compatibility checking)
    • org.mobilenativefoundation.store.formatting (Project formatting)

    It also configures compileSdk, minSdk, targetSdk, lint rules, testOptions, and compileOptions (Java/Kotlin JVM compatibility) using values from your project's versionCatalog.

  3. Apply the FormattingConventionPlugin to a Gradle project

    main

    The FormattingConventionPlugin automates code formatting by applying and configuring both ktlint and spotless.

    When applied, it performs the following actions:

    1. Applies the org.jlleitschuh.gradle.ktlint plugin.
    2. Applies the com.diffplug.spotless plugin and configures it to target Kotlin files in src/**/*.kt.
    3. Configures ktlint using the ktlint version defined in the libs version catalog.
    4. Disables several specific ktlint standard rules via additionalEditorconfig to match project conventions, including:
      • ktlint_standard_function-expression-body
      • ktlint_standard_class-signature
      • ktlint_standard_spacing-between-declarations-with-comments
      • ktlint_standard_when-entry-bracing
      • ktlint_standard_blank-line-between-when-conditions
      • ktlint_standard_kdoc
      • ktlint_standard_max-line-length
      • ktlint_standard_chain-method-continuation
      • ktlint_standard_function-signature
  4. Apply the KotlinMultiplatformConventionPlugin

    main

    The KotlinMultiplatformConventionPlugin is a Gradle plugin that applies a standardized set of conventions for Kotlin Multiplatform (KMP) projects within the mobilenativefoundation/store ecosystem.

    When applied, it automatically configures:

    • Kotlin Multiplatform: Applies org.jetbrains.kotlin.multiplatform, serialization, and native/cocoapods plugins.
    • Android: Configures the Android library target with specific SDK versions and lint rules.
    • Targets: Sets up jvm, iosX64, iosArm64, iosSimulatorArm64, linuxX64, js (browser/nodejs), and wasmJs (browser/nodejs).
    • Tooling: Configures Dokka for documentation, KmmBridge for artifact distribution, AtomicFU, and Maven Publish for publishing to Maven Central.
    • Compiler Options: Injects specific free compiler arguments like -Xexpect-actual-classes and -Xallocator=custom for native targets.

    Note: This plugin relies on a Gradle Version Catalog named libs to retrieve versions for the JVM toolchain, Android SDKs, and the project version.

  5. Build a cache using CacheBuilder

    main

    To create a cache, use CacheBuilder<K, V>() where K is your key type and V is your value type. You can configure properties such as maximumSize and expireAfterWrite before calling .build().

    Example with a Key data class and a Post data class:

    data class Key(
        val id: String
    )
    
    data class Post(
        val title: String
    )
    
    val cache = CacheBuilder<Key, Post>()
        .maximumSize(100)
        .expireAfterWrite(1.day)
        .build()
  6. Configure MemoryPolicy using MemoryPolicyBuilder

    main

    Use MemoryPolicy.builder() to configure the in-memory caching behavior for Store5. You can configure expiration policies, maximum size, or maximum weight.

    Important Constraints:

    • Expiration: You can set either setExpireAfterWrite or setExpireAfterAccess, but not both.
    • Sizing: You can set either setMaxSize (count-based) or setWeigherAndMaxWeight (weight-based), but not both.
    • Defaults: DEFAULT_DURATION_POLICY is Duration.INFINITE and DEFAULT_SIZE_POLICY is -1 (unlimited).
  7. Retrieve data from StoreMultiCache

    main

    Use the following methods to retrieve data from the cache:

    • getIfPresent(key: Key): Output?: Returns the cached value if it exists for the given key (either a StoreKey.Single or StoreKey.Collection), otherwise returns null.
    • getOrPut(key: Key, valueProducer: () -> Output): Output: Returns the cached value if present. If not, executes the valueProducer function, stores the result in the cache, and returns it.
    • getAllPresent(keys: List<*>): Map<Key, Output>: Returns a map of all keys in the list that are currently present in the cache.
    • getAllPresent(): Map<Key, Output>: Returns a map of all entries currently in the cache.
  8. Use StoreMultiCache for multi-layer caching

    main

    StoreMultiCache is a caching system that manages both single items and collections through decomposition. It allows you to store and retrieve data using StoreKey.Single or StoreKey.Collection keys. When you put a single item, it can automatically update the corresponding collection if a KeyProvider is provided.

    To instantiate it, you must provide a KeyProvider and optionally provide custom Cache instances for singles and collections (which default to using CacheBuilder).

  9. Create a new downstream flow with newDownstream()

    main

    Call newDownstream() to obtain a Flow<T> that collects values dispatched by the Multicaster's single upstream source.

    Parameters

    • piggybackOnly: If true, this downstream will not trigger a new upstream to start running. This is only valid if piggybackingDownstream was set to true during Multicaster initialization. If false (default), the downstream will start the upstream if no other upstream is currently running.
  10. Set expiration policies in MemoryPolicy

    main

    To control how long entries stay in the in-memory cache, use one of the following methods in the MemoryPolicyBuilder:

    1. Write-based expiration: Use setExpireAfterWrite(expireAfterWrite: Duration). The entry expires after a fixed duration since it was written.
    2. Access-based expiration: Use setExpireAfterAccess(expireAfterAccess: Duration). The entry expires after a fixed duration since it was last accessed.

    Note: You cannot use both policies simultaneously.