>我看到了一个可以存储有效载荷在内存缓存中并获得更快响应的库之一。
>
库名是api-cache
。
缓存路由
import express from 'express'import apicache from 'apicache'let app = express()let cache = apicache.middlewareapp.get('/api/collection/:id?', cache('5 minutes'), (req, res) => { // do some work... this will only occur once per 5 minutes res.json({ foo: 'bar' })})
缓存所有路由
let cache = apicache.middlewareapp.use(cache('5 minutes'))app.get('/will-be-cached', (req, res) => { res.json({ success: true })})
与redis一起使用
import express from 'express'import apicache from 'apicache'import redis from 'redis'let app = express()// if redisclient option is defined, apicache will use redis client// instead of built-in memory storelet cachewithredis = apicache.options({ redisclient: redis.createclient() }).middlewareapp.get('/will-be-cached', cachewithredis('5 minutes'), (req, res) => { res.json({ success: true })})
缓存组和手动控件
import apicache from 'apicache'let cache = apicache.middlewareapp.use(cache('5 minutes'))// routes are automatically added to index, but may be further added// to groups for quick deleting of collectionsapp.get('/api/:collection/:item?', (req, res) => { req.apicacheGroup = req.params.collection res.json({ success: true })})// add route to display cache performance (courtesy of @killdash9)app.get('/api/cache/performance', (req, res) => { res.json(apicache.getPerformance())})// add route to display cache indexapp.get('/api/cache/index', (req, res) => { res.json(apicache.getIndex())})// add route to manually clear target/groupapp.get('/api/cache/clear/:target?', (req, res) => { res.json(apicache.clear(req.params.target))})/*GET /api/foo/bar --> caches entry at /api/foo/bar and adds a group called 'foo' to indexGET /api/cache/index --> displays indexGET /api/cache/clear/foo --> clears all cached entries for 'foo' group/collection
>我建议您可以使用此api中间件。在该数据库中很少更新。像国家,城市和验证者api数据一样。
*/
以上就是Nodejs中最好的API缓存lib的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1501116.html
微信扫一扫
支付宝扫一扫