key = cacheKey
}
// 如果缓存命中,直接返回
const value = cacheStore.get(key)
if (value) {
return res.end(value, 'utf-8')
}
// 缓存原先的end方案
res.original_end = res.end
// 重写res.end方案,由此nuxt调用res.end实际上是调用该方法,
res.end = function () {
if (res.statusCode === 200) {
// 设置缓存
cacheStore.set(key, data, ttl)
}
// 最终返回结果
res.original_end(data, 'utf-8')
}
}
}
如果缓存命中,直接将原先的计算结果返回,大大提供了性能
总结
在高并发的情况下可以考虑使用缓存,而缓存策略的使用需要视场景而定,这里不再赘述,还可以考虑使用pm2开启集群模式去管理我们的进程,从而满足更高的并发。










