-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhttp_cache.rb
More file actions
37 lines (31 loc) · 1.04 KB
/
http_cache.rb
File metadata and controls
37 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true
require 'time'
module Html2rss
module Web
##
# Collection of methods which set HTTP Caching related headers in the response.
module HttpCache
module_function
##
# Sets Expires and Cache-Control headers to cache for `seconds`.
# @param response [Hash]
# @param seconds [Integer]
# @param cache_control [String, nil]
def expires(response, seconds, cache_control: nil)
expires_now(response) and return if seconds <= 0
response['Expires'] = (Time.now + seconds).httpdate
cache_value = "max-age=#{seconds}"
cache_value += ",#{cache_control}" if cache_control
response['Cache-Control'] = cache_value
end
##
# Sets Expires and Cache-Control headers to invalidate existing cache and
# prevent caching.
# @param response [Hash]
def expires_now(response)
response['Expires'] = '0'
response['Cache-Control'] = 'private,max-age=0,no-cache,no-store,must-revalidate'
end
end
end
end