Ruby on Rails:如果是AJAX请求,如何将内容过期设置为15分钟

时间:2021-08-24 02:16:51

Is the following a good form of detecting AJAX request and setting expiration for 15 minutes so that the same GET will not require any network traffic?

以下是检测AJAX请求并设置15分钟到期的良好形式,以便相同的GET不需要任何网络流量吗?

# in controller
if request.xhr?
  response.headers['Expires'] = (Time.now + 15.minutes).httpdate
  response.headers['Cache-Control'] = ''   # override any no-cache, must-revalidate
end

Update: I found a shorter alternative, which is expires_in

更新:我找到了一个更短的替代品,即expires_in

# in controller
if request.xhr?
  expires_in(15.minutes)
end

although, after that, the header becomes:

虽然在那之后,标题变成:

Cache-Control: max-age=900, private

which used to be

曾经是

Expires: Tue, 13 Jul 2010 00:59:47 GMT

when it was the earlier version. But note that Cache-Control is for HTTP/1.1 and even a couple years ago, it was supported by 99% of the browsers, as mentioned in the High Performance Website book.

当它是早期版本。但请注意,Cache-Control适用于HTTP / 1.1,甚至几年前,99%的浏览器都支持它,如高性能网站书中所述。

1 个解决方案

#1


0  

Ok, since there is no answer yet, I am going to use the one in the update above:

好的,因为还没有答案,我将使用上面更新中的那个:

if request.xhr?
  expires_in(15.minutes)
end

I tried it and it works well.

我试了一下,效果很好。

#1


0  

Ok, since there is no answer yet, I am going to use the one in the update above:

好的,因为还没有答案,我将使用上面更新中的那个:

if request.xhr?
  expires_in(15.minutes)
end

I tried it and it works well.

我试了一下,效果很好。