module Roda::RodaPlugins::Caching::ResponseMethods

  1. lib/roda/plugins/caching.rb

Methods

Public Instance

  1. cache_control
  2. expires
  3. finish

Public Instance methods

cache_control(opts)

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

[show source]
    # File lib/roda/plugins/caching.rb
171 def cache_control(opts)
172   values = []
173   opts.each do |k, v|
174     next unless v
175     k = k.to_s.tr('_', '-')
176     values << (v == true ? k : "#{k}=#{v}")
177   end
178 
179   @headers[RodaResponseHeaders::CACHE_CONTROL] = values.join(', ') unless values.empty?
180 end
expires(max_age, opts=OPTS)

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).

[show source]
    # File lib/roda/plugins/caching.rb
186 def expires(max_age, opts=OPTS)
187   cache_control(Hash[opts].merge!(:max_age=>max_age))
188   @headers[RodaResponseHeaders::EXPIRES] = (Time.now + max_age).httpdate
189 end
finish()

Remove Content-Type and Content-Length for 304 responses.

[show source]
    # File lib/roda/plugins/caching.rb
192 def finish
193   a = super
194   if a[0] == 304
195     h = a[1]
196     h.delete(RodaResponseHeaders::CONTENT_TYPE)
197     h.delete(RodaResponseHeaders::CONTENT_LENGTH)
198   end
199   a
200 end