前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nestJS 高速缓存用法

nestJS 高速缓存用法

作者头像
stormKid
发布2019-03-11 10:59:36
1.9K0
发布2019-03-11 10:59:36
举报
文章被收录于专栏:计算机编程计算机编程

前言:官方文档在这方面还是没有讲清楚什么使用,怎么应用,只是给了个大概的配置和大致的使用方式,限制在单数据流缓存get请求的数据上面,并没有实质意义上用到缓存的处理。于是博主花了接近一个星期翻看源码以及逐步测试,发现实际上使用真的很简单,这里确实要夸一下nestjs的封装。接下来博主就以验证码缓存来着重介绍其用法。

1、源码分析:

通过官方文档,我们知道要写个拦截器继承CacheInterceptor进行对服务的交互处理,于是让我们查看一下它做了什么操作:

代码语言:javascript
复制
let CacheInterceptor = class CacheInterceptor {
    constructor(cacheManager, reflector) {
        this.cacheManager = cacheManager;
        this.reflector = reflector;
    }
    async intercept(context, call$) {
        const key = this.trackBy(context);
        if (!key) {
            return call$;
        }
        try {
            const value = await this.cacheManager.get(key);
            if (value) {
                return rxjs_1.of(value);
            }
            return call$.pipe(operators_1.tap(response => this.cacheManager.set(key, response)));
        }
        catch (_a) {
            return call$;
        }
    }
    trackBy(context) {
        const httpServer = this.applicationRefHost.applicationRef;
        const isHttpApp = httpServer && !!httpServer.getRequestMethod;
        if (!isHttpApp) {
            return this.reflector.get(cache_constants_1.CACHE_KEY_METADATA, context.getHandler());
        }
        const request = context.getArgByIndex(0);
        if (httpServer.getRequestMethod(request) !== 'GET') {
            return undefined;
        }
        return httpServer.getRequestUrl(request);
    }
};
1.1、trackBy()

此方法传递一个context上下文进来,通过上下文获取我们请求的对象,如果是get请求,则找到此时在请求阶段promise的CacheKey的值;如果是post或其他请求方式则不做操作。

1.2、intercept()

此方法对服务端回数据制做进一步的处理,通过rxjs的分发机制,我们可以了解到,它分发了我们此次请求回来的response数据,同时也通过构造中的cacheManager来保存了当前的response。更为重要的是根据它自定义的trackBy方法获取了之前的key值,这点很关键。我们后面获取数据时的必要条件。

2、自定义拦截器:

2.1、需求

在我们开始编程的时候必须先理清楚我们需求的功能,我们需要缓存此次的验证码,并且通过key再次获取它进行进一步的验证。

2.2、分别建立两个拦截器:

PutCodeInterceptor

代码语言:javascript
复制
@Injectable()
export class PutCodeInterceptor extends CacheInterceptor {
   trackBy(context: ExecutionContext) {
     const req = context.switchToHttp().getRequest();
     return 'smsKey';
   }
}

GetCodeInterceptor

代码语言:javascript
复制
@Injectable()
export class GetCodeInterceptor extends CacheInterceptor {
  async intercept(context: ExecutionContext, call$: Observable<any>): Promise<Observable<any>> {
    const result = await this.cacheManager.get('smsKey');
    return call$.pipe(map(data => ({ result })));
  }
}

分析: PutCodeInterceptor 将key值通过request来进行获取,这里我简单的写死为'smsKey',这样便可以通过CacheIntercepter的 intercept方法获取此key来根据key缓存response。 然后作为再次获取此缓存的value ,再次添加GetCodeInterceptor 重写 CacheIntercepter 的 intercept方法 ,最终通过cacheManager的get()方法获取方才缓存的response,最终可以通过缓存的response来进行判断处理,我这里为了方便起见直接当result返回了。

3、使用:

代码语言:javascript
复制
@Controller('sms')
export class SmsController{
    constructor(private log: Log, private app: AppService) { }
    @Get('put')
    @UseInterceptors(PutCodeInterceptor)
    async testCode() {
        const rep: DoctorInfo = {
            name: '你好',
            code: this.app.createCode(),
        };
        return rep;
    }

    @Post('test')
    @UseInterceptors(GetCodeInterceptor)
    async test() {
    }

}

最终测试效果:

put:

test:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.02.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、源码分析:
    • 1.1、trackBy()
      • 1.2、intercept()
      • 2、自定义拦截器:
        • 2.1、需求
          • 2.2、分别建立两个拦截器:
          • 3、使用:
          相关产品与服务
          验证码
          腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档