来自 Google 云存储的快速缓存图像流
Express caching Image stream from Google Cloud Storage
我正在使用 express 4 提供图像流:
app.get('/v1/images/:Uid/', function(req, res) {
var gcsbucket = gcs.bucket('bucket'),
remoteReadStream = gcsbucket.file(req.params.Uid+'.jpg').createReadStream();
remoteReadStream.pipe(res);
});
(图像存储在 Google 云存储中)
我正在尝试找到一种方法让浏览器缓存我提供的图像。
我试图添加:
res.set('Cache-Control', 'public, max-age=31557600');
但是当我在 http://server/v1/images/1234 访问 API 时,这并没有改变任何东西,它总是重新加载图像。
在使用流服务器端时告诉浏览器缓存响应的正确方法是什么?
这确实有效:
app.get('/v1/images/:Uid/', function(req, res) {
var gcsbucket = gcs.bucket('bucket'),
remoteReadStream = gcsbucket.file(req.params.Uid+'.jpg').createReadStream();
res.set('Cache-Control', 'public, max-age=31557600');
remoteReadStream.pipe(res);
});
奇怪的是,当您直接访问 API 时,浏览器从不缓存。但是,当您将图像嵌入 html 页面时,图像会被缓存,例如 :
<img src="http://server/v1/images/1234">
这对我的用例来说已经足够了,但如果有人知道为什么?
我正在使用 express 4 提供图像流:
app.get('/v1/images/:Uid/', function(req, res) {
var gcsbucket = gcs.bucket('bucket'),
remoteReadStream = gcsbucket.file(req.params.Uid+'.jpg').createReadStream();
remoteReadStream.pipe(res);
});
(图像存储在 Google 云存储中)
我正在尝试找到一种方法让浏览器缓存我提供的图像。 我试图添加:
res.set('Cache-Control', 'public, max-age=31557600');
但是当我在 http://server/v1/images/1234 访问 API 时,这并没有改变任何东西,它总是重新加载图像。
在使用流服务器端时告诉浏览器缓存响应的正确方法是什么?
这确实有效:
app.get('/v1/images/:Uid/', function(req, res) {
var gcsbucket = gcs.bucket('bucket'),
remoteReadStream = gcsbucket.file(req.params.Uid+'.jpg').createReadStream();
res.set('Cache-Control', 'public, max-age=31557600');
remoteReadStream.pipe(res);
});
奇怪的是,当您直接访问 API 时,浏览器从不缓存。但是,当您将图像嵌入 html 页面时,图像会被缓存,例如 :
<img src="http://server/v1/images/1234">
这对我的用例来说已经足够了,但如果有人知道为什么?