Data
Caching
The cache service and interceptor, @Cacheable methods, and stores for memory, Caffeine, files, Redis and Memcached, alone or layered.
cache is @nestjs/cache-manager: a cache service over a pluggable byte store, an interceptor for responses, and method decorators.
Kest.run<AppModule> { import(CacheModule.register { ttl = Duration.ofSeconds(60); store = CaffeineCacheStore(maxEntries = 50_000) }) }Using it
@Injectable
class Catalog(private val cache: CacheService, private val repo: ProductRepository) {
fun product(id: String): Product? = cache.wrap("product:$id", Duration.ofMinutes(5)) { repo.findById(id) ?: return null }
fun invalidate(id: String) = cache.delete("product:$id")
}get, set, delete, clear, has, wrap(key, ttl) { compute }. Values are stored as JSON through the application's codec, so any store works.
Whole responses
@UseInterceptors(CacheInterceptor::class) @CacheTtl(30) @Get("/products") fun list() = ...Caches successful GETs by URL; a hit skips the handler and carries x-cache: HIT. @CacheKey("all") sets the key, @NoCache exempts a handler when the interceptor is global.
Method decorators
@Injectable
open class Directory {
@Cacheable("profile:{0}", ttlSeconds = 60) open fun find(id: String): Profile? = ...
@CachePut("profile:{0}") open fun rename(id: String, name: String): Profile = ...
@CacheEvict("profile:{0}") open fun forget(id: String) { ... }
@CacheEvict(allEntries = true) open fun forgetAll() { ... }
}{0}, {1}... in the key are the arguments (no key: Class.method: plus all of them). A null result is not cached. A collection result is decoded back as the declared type. Like every method decorator it needs an open class and methods. Several threads that miss at the same moment may each compute the value once.
Stores
| Store | Package | Shared between instances | Notes |
|---|---|---|---|
InMemoryCacheStore(maxEntries) | cache | no | LRU, the default |
CaffeineCacheStore(maxEntries) | cache-caffeine | no | lock-free reads, Window TinyLFU eviction, a TTL per entry |
FileCacheStore(dir) | cache | no | one file per key, survives restarts, atomic writes |
RedisCacheStore(host, port, prefix) | cache-redis | yes | PX expiry, scans only its own prefix |
MemcachedCacheStore(servers, prefix) | cache-memcached | yes | clear() bumps a version, so it never flushes a server others use; cannot list keys |
LayeredCacheStore(near, far, nearTtl) | cache | via far | a small local store in front of a shared one |
store = LayeredCacheStore(near = CaffeineCacheStore(5_000), far = RedisCacheStore("redis", 6379), nearTtl = Duration.ofSeconds(20))Reads try the near store, fall back to the far one and fill the near one; writes and deletes go to both. Keep nearTtl short so an instance does not serve what another has already replaced.
Any other store (MongoDB, DynamoDB, a database table) is a few lines: implement get, set(key, bytes, ttl), delete, clear, keys.
What is tested
One shared contract (miss, overwrite, TTL expiry, awkward keys, binary values, delete, clear, 16 writer threads) runs against all of these, including live Redis and Memcached, a Caffeine-over-Redis layer, and two Memcached clients sharing one cache.