A cache module for Nest framework (node.js) https://nestjs.com/
$ npm i --save @anchan828/nest-cache
- Import module
@Module({
imports: [CacheModule.register()],
})
export class AppModule {}
@Injectable()
export class ExampleService {
constructor(private readonly cacheService: CacheService) {}
private items: Item[] = Array(5)
.fill(0)
.map((_, index) => ({ id: index, name: `Item ${index}` }));
public async getItems(userId: number): Promise<Item[]> {
const cacheKey = `users/${userId}/items`;
const cache = await this.cacheService.get<Item[]>(cacheKey);
if (cache) {
return cache;
}
await this.cacheService.set(cacheKey, this.items);
return this.items;
}
}
@anchan828/nest-cache has been extended to make more Redis commands available. In line with this, the memory store also provides compatibility features. Please use @anchan828/nest-cache-manager-memory instead of the default memory store.
import { memoryStore } from "@anchan828/nest-cache-manager-memory";
@Module({
imports: [
CacheModule.register({
store: memoryStore,
}),
],
})
export class AppModule {}
You can use Redis instead of in-memory cache. Please use @anchan828/nest-cache-manager-ioredis
@anchan828/nest-cache-manager-ioredis has the ability to cache Redis results in AsyncLocalStorage. This is useful for elements that need to be accessed frequently.
import { redisStore } from "@anchan828/nest-cache-manager-ioredis";
const asyncLocalStorage = new AsyncLocalStorage<Map<string, any>>();
@Module({
imports: [
CacheModule.register({
store: redisStore,
host: "localhost",
asyncLocalStorage,
}),
],
})
export class AppModule {}
- hget
- hset
- hdel
- hgetall
- hkeys