Class: Rack::StaticCache
- Inherits:
-
Object
- Object
- Rack::StaticCache
- Defined in:
- lib/rack/contrib/static_cache.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #duration_in_seconds ⇒ Object
- #duration_in_words ⇒ Object
-
#initialize(app, options = {}) ⇒ StaticCache
constructor
A new instance of StaticCache.
Constructor Details
#initialize(app, options = {}) ⇒ StaticCache
Returns a new instance of StaticCache.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rack/contrib/static_cache.rb', line 52 def initialize(app, ={}) @app = app @urls = [:urls] @no_cache = {} @urls.collect! do |url| if url =~ /\*$/ url.sub!(/\*$/, '') @no_cache[url] = 1 end url end root = [:root] || Dir.pwd @file_server = Rack::File.new(root) @cache_duration = [:duration] || 1 @versioning_enabled = .fetch(:versioning, true) if @versioning_enabled @version_regex = .fetch(:version_regex, /-[\d.]+([.][a-zA-Z][\w]+)?$/) end @duration_in_seconds = self.duration_in_seconds @duration_in_words = self.duration_in_words end |
Instance Method Details
#call(env) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rack/contrib/static_cache.rb', line 74 def call(env) path = env["PATH_INFO"] url = @urls.detect{ |u| path.index(u) == 0 } if url.nil? @app.call(env) else if @versioning_enabled path.sub!(@version_regex, '\1') end status, headers, body = @file_server.call(env) if @no_cache[url].nil? headers['Cache-Control'] ="max-age=#{@duration_in_seconds}, public" headers['Expires'] = @duration_in_words end [status, headers, body] end end |
#duration_in_seconds ⇒ Object
96 97 98 |
# File 'lib/rack/contrib/static_cache.rb', line 96 def duration_in_seconds (60 * 60 * 24 * 365 * @cache_duration).to_i end |
#duration_in_words ⇒ Object
92 93 94 |
# File 'lib/rack/contrib/static_cache.rb', line 92 def duration_in_words (Time.now + self.duration_in_seconds).strftime '%a, %d %b %Y %H:%M:%S GMT' end |