Caching

CacheManager, ServiceCache, RequestCache, two-tier architecture, cache invalidation, and deferred writes.

Two-Tier Architecture

Bosca uses a two-tier cache with transaction-aware deferred writes:

  1. Tier 1: RequestCache — in-memory ConcurrentHashMap per request. Prevents redundant deserialization and remote lookups within a single request.
  2. Tier 2: CacheManager — distributed cache shared across all server instances. Backing store is configurable (Redis, NATS KV).
Remote cache writes are deferred until after the database transaction commits. If the transaction rolls back, cache updates are discarded. See Transactions.

ServiceCache

The primary caching abstraction used in services. Each instance represents a named cache slot.

Simple cache (single value)

kotlin

Keyed cache with batch resolver

kotlin

Constructor parameters

ParameterPurpose
cacheNameUnique name in CacheManager; used as key prefix in the distributed store
serializerKey serializer (UUIDKeySerializer, StringKeySerializer, UnitKeySerializer, etc.)
batchResolverResolves multiple keys in one query for DataLoader integration
expirationTTL in the distributed cache (default: 10 minutes)
resolverResolves a single key on cache miss

Cache Invalidation

Every cache that could contain stale data must be evicted. When you have multiple caches over the same data, you must invalidate all of them.
kotlin

Memory Management in Batch Jobs

Clear the local request cache periodically to prevent unbounded memory growth:

kotlin