-
Notifications
You must be signed in to change notification settings - Fork 43
/
cacheable.rb
81 lines (67 loc) · 2.04 KB
/
cacheable.rb
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Expires
def do_http_cache(responder, headers)
if responder.options[:expires_in]
headers << "max-age=#{responder.options[:expires_in]}"
true
else
false
end
end
end
class LastModifieds
# default implementation that will check whether caching can be applied
def accepts?(responder)
responder.resources.flatten.select do |resource|
resource.respond_to?(:updated_at)
end &&
responder.controller.response.last_modified.nil? &&
!new_record?(responder)
end
def do_http_cache(responder, headers)
return false unless accepts?(responder)
timestamps = responder.resources.flatten.select do |resource|
resource.respond_to?(:updated_at)
end.map do |resource|
(resource.updated_at || Time.now).utc
end
timestamp = timestamps.max
responder.controller.response.last_modified = timestamp if timestamp
end
def new_record?(responder)
responder.resource.respond_to?(:new_record?) && responder.resource.new_record?
end
end
module Restfulie::Server::ActionController
module Trait
module Cacheable
CACHES = [::Expires.new, ::LastModifieds.new]
def to_format
headers = cache_control_headers
cached = CACHES.inject(false) do |cached, cache|
cached || cache.do_http_cache(self, headers)
end
if ::ActionController::Base.perform_caching && cached
set_public_cache_control(headers)
controller.response.headers["Cache-Control"] = headers.join(', ')
fresh = request.fresh?(controller.response)
if fresh
head :not_modified
else
super
end
else
super
end
end
private
def set_public_cache_control(headers)
headers.delete("private")
headers.delete("no-cache")
headers << "public"
end
def cache_control_headers
(controller.response.headers["Cache-Control"] || "").split(",").map {|k| k.strip }
end
end
end
end