Module: Roda::RodaPlugins::Caching::ResponseMethods
- Defined in:
- lib/roda/plugins/caching.rb
Instance Method Summary collapse
-
#cache_control(opts) ⇒ Object
Specify response freshness policy for using the Cache-Control header.
-
#expires(max_age, opts = OPTS) ⇒ Object
Set Cache-Control header with the max_age given.
-
#finish ⇒ Object
Remove Content-Type and Content-Length for 304 responses.
Instance Method Details
#cache_control(opts) ⇒ Object
Specify response freshness policy for using the Cache-Control header. Options can can any non-value directives (:public, :private, :no_cache, :no_store, :must_revalidate, :proxy_revalidate), with true as the value. Options can also contain value directives (:max_age, :s_maxage).
response.cache_control public: true, max_age: 60
# => Cache-Control: public, max-age=60
See RFC 2616 / 14.9 for more on standard cache control directives: tools.ietf.org/html/rfc2616#section-14.9.1
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/roda/plugins/caching.rb', line 171 def cache_control(opts) values = [] opts.each do |k, v| next unless v k = k.to_s.tr('_', '-') values << (v == true ? k : "#{k}=#{v}") end @headers[RodaResponseHeaders::CACHE_CONTROL] = values.join(', ') unless values.empty? end |
#expires(max_age, opts = OPTS) ⇒ Object
Set Cache-Control header with the max_age given. max_age should be an integer number of seconds that the current request should be cached for. Also sets the Expires header, useful if you have HTTP 1.0 clients (Cache-Control is an HTTP 1.1 header).
186 187 188 189 |
# File 'lib/roda/plugins/caching.rb', line 186 def expires(max_age, opts=OPTS) cache_control(Hash[opts].merge!(:max_age=>max_age)) @headers[RodaResponseHeaders::EXPIRES] = (Time.now + max_age).httpdate end |
#finish ⇒ Object
Remove Content-Type and Content-Length for 304 responses.
192 193 194 195 196 197 198 199 200 |
# File 'lib/roda/plugins/caching.rb', line 192 def finish a = super if a[0] == 304 h = a[1] h.delete(RodaResponseHeaders::CONTENT_TYPE) h.delete(RodaResponseHeaders::CONTENT_LENGTH) end a end |