Back-end/Node-NestJS
Nest.js node-cache 라이브러리 적용
꼬바리
2023. 3. 21. 18:24
휴대폰 인증같이 간단한 처리는 캐시에 데이터를 넣어 처리 가능하다.
간단하게
캐시데이터 get / set 코드
import { Injectable } from '@nestjs/common';
import NodeCache from 'node-cache';
@Injectable()
export class AuthCache {
private cache: NodeCache;
constructor() {
this.cache = new NodeCache();
}
async createCache(params : any) {
let { props } = params
let { mobileNo, authNo } = props
const cacheKey = mobileNo; // 캐시 키
const chcheValue = authNo // 캐시 값
let isSuccess = this.cache.set(cacheKey, chcheValue, 60 * 3);
// set('cache 키', 'cache 값', 만료시간(sec))
return isSuccess;
}
async checkCache(params : any) {
let { props } = params
let { mobileNo, authNo } = props
const cacheKey = mobileNo;
let value = this.cache.get<string>(cacheKey);
return value;
}
}
728x90
반응형