>我看到了一个可以存储有效载荷在内存缓存中并获得更快响应的库之一。
>库名是api-cache
。缓存路由
import express from 'express'
import apicache from 'apicache'
let app = express()
let cache = apicache.middleware
app.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.middleware
app.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 store
let cachewithredis = apicache.options({ redisclient: redis.createclient() }).middleware
app.get('/will-be-cached', cachewithredis('5 minutes'), (req, res) => {
res.json({ success: true })
})
缓存组和手动控件
import apicache from 'apicache'
let cache = apicache.middleware
app.use(cache('5 minutes'))
// routes are automatically added to index, but may be further added
// to groups for quick deleting of collections
app.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 index
app.get('/api/cache/index', (req, res) => {
res.json(apicache.getIndex())
})
// add route to manually clear target/group
app.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 index
GET /api/cache/index --> displays index
GET /api/cache/clear/foo --> clears all cached entries for 'foo' group/collection
>我建议您可以使用此api中间件。在该数据库中很少更新。像国家,城市和验证者api数据一样。
*/

cozeniths.com









