yPh*_*hil 3 caching node.js express
该Web应用程序从Web检索图像,尤其是图标图标。因此,每次刷新页面时,都会向网站发出请求,然后再次获取图标。
这是我传递URL的功能;favicon()是一个从网站/页面抓取并返回它的库。
router.get('/feedicon', function(req, res) {
favicon(req.query.url, function(err, icon) {
if (icon) {
res.send(icon);
} else {
res.status(500).send('No icon found');
}
});
});
Run Code Online (Sandbox Code Playgroud)
现在,如果icon找到有效文件,
/cache目录存在/创建目录(如果不可能,则返回错误);icon在/cache目录中;我宁愿不安装lib来做到这一点,实际上,我绊脚石的地方是“唯一时间戳”缓存图像的最佳方法,从而确保提供正确的文件。
另外,如果使用哈希将文件标识为ID,我还会担心性能。
快速缓存这样的文件的最佳实践是什么?我很难找到它,就像没有人需要在NodeJs / express中缓存动态图像一样。
这是一个缓存public我存储图像的文件夹的简单示例
const cacheTime = 86400000 * 30 // the time you want
const path = require('path')
.
.
.
.
app.use(express.static(path.join(__dirname, 'public'), {
maxAge: cacheTime
}))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1984 次 |
| 最近记录: |