菜鸟总是要踩很多坑,记录坑让其他菜鸟少踩。
一、安装依赖
npm install cache-manager --save
npm install cache-manager-redis-store --save
npm install @types/cache-manager -D
二、redis-cache.service.ts
import { Injectable, Inject, CACHE_MANAGER } from '@nestjs/common';
import { Cache } from 'cache-manager';
@Injectable()
export class RedisCacheService {
constructor(
@Inject(CACHE_MANAGER)
private cacheManager: Cache
) {
}
cacheSet(key: string, value: string, ttl: number) {
this.cacheManager.set(key, value, { ttl: ttl }, (err: any) => {
if (err) {
throw err;
}
})
}
async cacheGet(key: any): Promise<any> {
return this.cacheManager.get(key);
}
}
三、redis-cache.module.ts
import { Module, CacheModule } from '@nestjs/common';
import * as redisStore f