CacheManager

interface CacheManager

CacheManager exposes a high-level API to access a com.apollographql.cache.normalized.api.NormalizedCache.

Although all operations are suspend functions, they may suspend or block the thread depending on the underlying cache implementation. For example, the SQL cache implementation on Android will block the thread while accessing the disk. As such, these operations must not run on the main thread. You can enclose them in a kotlinx.coroutines.withContext block with a Dispatchers.IO context to ensure that they run on a background thread.

Note that changes are not automatically published - pass publish = true or call publish to notify any watchers.

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class ReadResult<D : Executable.Data>(val data: D, val cacheHeaders: CacheHeaders)

Properties

Link copied to clipboard
abstract val changedKeys: SharedFlow<Set<String>>

Exposes the record field keys that have changed. This is collected internally to notify watchers.

Functions

Link copied to clipboard
abstract suspend fun <R> accessCache(block: suspend (NormalizedCache) -> R): R

Direct access to the cache.

Link copied to clipboard
abstract suspend fun clearAll(publish: Boolean = false): Boolean

Clears all records.

Link copied to clipboard
abstract fun dispose()

Releases resources associated with this store.

Link copied to clipboard
abstract suspend fun dump(): Map<KClass<*>, Map<CacheKey, Record>>

Dumps the content of the store for debugging purposes.

Link copied to clipboard
abstract fun <D : Executable.Data> normalize(executable: Executable<D>, dataWithErrors: DataWithErrors, rootKey: CacheKey = CacheKey.QUERY_ROOT, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty): Map<CacheKey, Record>

Normalizes executable data to a map of Record keyed by Record.key.

Link copied to clipboard
abstract suspend fun publish(keys: Set<String>)

Publishes a set of keys of record fields that have changed. This will notify watchers and any subscribers of changedKeys.

Link copied to clipboard
abstract suspend fun <D : Fragment.Data> readFragment(fragment: Fragment<D>, cacheKey: CacheKey, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, cacheHeaders: CacheHeaders = CacheHeaders.NONE): CacheManager.ReadResult<D>

Reads a fragment from the store.

Link copied to clipboard
abstract suspend fun <D : Operation.Data> readOperation(operation: Operation<D>, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, cacheHeaders: CacheHeaders = CacheHeaders.NONE): ApolloResponse<D>

Reads an operation from the store.

Link copied to clipboard
abstract suspend fun remove(cacheKey: CacheKey, cascade: Boolean = true): Boolean

Removes a record by its key.

abstract suspend fun remove(cacheKeys: List<CacheKey>, cascade: Boolean = true): Int

Removes a list of records by their keys. This is an optimized version of remove for caches that can batch operations.

Link copied to clipboard
abstract suspend fun rollbackOptimisticUpdates(mutationId: Uuid, publish: Boolean = false): Set<String>

Rollbacks optimistic updates.

Link copied to clipboard
abstract suspend fun trim(maxSizeBytes: Long, trimFactor: Float = 0.1f): Long

Trims the store if its size exceeds maxSizeBytes. The amount of data to remove is determined by trimFactor. The oldest records are removed according to their updated date.

Link copied to clipboard
abstract suspend fun <D : Fragment.Data> writeFragment(fragment: Fragment<D>, cacheKey: CacheKey, data: D, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, cacheHeaders: CacheHeaders = CacheHeaders.NONE, publish: Boolean = false): Set<String>

Writes a fragment to the store.

Link copied to clipboard
abstract suspend fun <D : Operation.Data> writeOperation(operation: Operation<D>, dataWithErrors: DataWithErrors, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, cacheHeaders: CacheHeaders = CacheHeaders.NONE, publish: Boolean = false): Set<String>
abstract suspend fun <D : Operation.Data> writeOperation(operation: Operation<D>, data: D, errors: List<Error>? = null, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, cacheHeaders: CacheHeaders = CacheHeaders.NONE, publish: Boolean = false): Set<String>

Writes an operation to the store.

Link copied to clipboard
abstract suspend fun <D : Operation.Data> writeOptimisticUpdates(operation: Operation<D>, data: D, mutationId: Uuid, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, publish: Boolean = false): Set<String>

Writes an operation to the optimistic store.

abstract suspend fun <D : Fragment.Data> writeOptimisticUpdates(fragment: Fragment<D>, cacheKey: CacheKey, data: D, mutationId: Uuid, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, publish: Boolean = false): Set<String>

Writes a fragment to the optimistic store.