Class: ZippyStaticCache

Inherits:
Object
  • Object
show all
Defined in:
lib/zippy_static_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ZippyStaticCache

Returns a new instance of ZippyStaticCache.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/zippy_static_cache.rb', line 2

def initialize(app, options={})
  @app = app
  @urls = options[:urls]
  @no_cache = {}
  @urls.collect! do |url|
    if url  =~ /\*$/
      url.sub!(/\*$/, '')
      @no_cache[url] = 1
    end
    url
  end
  @cache_duration = options[:duration] || 1
  @duration_in_seconds = self.duration_in_seconds
  @duration_in_words    = self.duration_in_words
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zippy_static_cache.rb', line 18

def call(env)
  path = env["PATH_INFO"]
  url = @urls.detect{ |u| path.index(u) == 0 }
  status, headers, body = @app.call(env)

  if !url.nil? && @no_cache[url].nil?
    headers['Cache-Control'] ="max-age=#{@duration_in_seconds}, public"
    headers['Expires'] = @duration_in_words
    headers.delete 'Etag'
    headers.delete 'Pragma'
    headers.delete 'Last-Modified'
  end

  [status, headers, body]
end

#duration_in_secondsObject



38
39
40
# File 'lib/zippy_static_cache.rb', line 38

def duration_in_seconds
  (60 * 60 * 24 * 365 * @cache_duration).to_i
end

#duration_in_wordsObject



34
35
36
# File 'lib/zippy_static_cache.rb', line 34

def duration_in_words
  (Time.now + self.duration_in_seconds).strftime '%a, %d %b %Y %H:%M:%S GMT'
end