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:
- Tier 1: RequestCache — in-memory
ConcurrentHashMapper request. Prevents redundant deserialization and remote lookups within a single request. - 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
| Parameter | Purpose |
|---|---|
cacheName | Unique name in CacheManager; used as key prefix in the distributed store |
serializer | Key serializer (UUIDKeySerializer, StringKeySerializer, UnitKeySerializer, etc.) |
batchResolver | Resolves multiple keys in one query for DataLoader integration |
expiration | TTL in the distributed cache (default: 10 minutes) |
resolver | Resolves 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