Caching
Enable the cache on the app. It is in-memory by default:
app.useCache({ provider: 'memory' });For Redis, pass connection options. The Redis client ships with @meghjs/core, and Megh opens the connection on first use and closes it on shutdown:
app.useCache({
provider: 'redis',
redis: { url: process.env.REDIS_URL }, // redis:// or rediss://
});redis also takes host, port (default 127.0.0.1:6379), username, password, db and tls.
Using the cache
Inject CacheManager. set takes a key, a value, a TTL in seconds, and optional tags:
import { AppService, CacheManager } from '@meghjs/core';
@AppService
export class UsersService {
constructor(private readonly cache: CacheManager) {}
async findAll() {
const cached = await this.cache.get<User[]>('users:all');
if (cached) return cached;
const users = await this.repo.find();
await this.cache.set('users:all', users, 3600, ['users']);
return users;
}
async update(user: User) {
await this.repo.save(user);
await this.cache.invalidateTags('users'); // drops every key tagged 'users'
}
}Expiry
- A TTL of
0or less means the entry never expires. - The memory store drops expired entries when they are read, and in a background sweep every 60 seconds. The sweep does not keep the process alive.
- The memory store has no size limit, so give large or numerous entries a TTL.
- In Redis, tag sets expire no earlier than the longest-lived key they track.
Instances
The CacheManager registered by useCache is a singleton shared through dependency injection. new CacheManager(options) creates an independent store, which is useful in tests:
const cache = new CacheManager({ provider: 'memory', sweepIntervalMs: 1000 });
cache.sweep(); // remove expired entries now; returns how many
await cache.close(); // stop the sweep timer and release resourcesCall await CacheManager.close() on shutdown to close every instance, including the Redis connection. The app does this for you when it shuts down.