BoscaDeveloper Documentation

Caching

Use request-local and distributed caches through Bosca's service abstractions.

Two cache tiers

  1. RequestCache: stores values locally for one request scope, avoiding repeated remote lookups and deserialization.
  2. CacheManager: manages named distributed caches shared by application instances. Redis and NATS KV implementations are available.

Services normally use ServiceCache, which accesses both tiers through the current request cache. Backend selection belongs in configuration.

A cache for one value

The following excerpts come from CategoryServiceImpl. The resolver loads the value when it is missing. UnitKeySerializer is useful for a single cached list.

kotlin

A cache for IDs and batched reads

Use a batch resolver to load multiple missing keys together. getAll() and GraphQL addToBatch() use this resolver. A cache used for batch reads needs a batch resolver; the default reports that batch support is unavailable.

kotlin

The ServiceCache factory takes a cache name, key serializer, optional batch resolver, expiration, and single-key resolver. Its default expiration is ten minutes. Import key serializers from bosca.cache.serializers.

Invalidate every affected cache

An edit can make both the full list and the ID entry stale. In this excerpt, existing is the category already loaded by the service:

kotlin
Run broad clear() operations before per-key remove() or put() operations. Clearing a named cache also clears the request's local state and pending cache operations.

Flush timing

Request-cache puts and removes queue remote operations. They flush through connection callbacks on commit or release, or immediately if there is no connection manager. Whole-cache clear() reaches the distributed cache immediately.

Do not assume a database rollback discards every pending cache operation: the request cache also flushes at connection release. See Transactions & Connections for the database lifecycle.

Establish a request-cache scope

HTTP handlers and GraphQL execution supply a request cache. Background listeners invoking cached services must establish one with withRequestCache { }, together with withConnectionManager { } when SQL is involved.