Class: Serve::ResponseCache

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

Constant Summary collapse

@@defaults =
{
  :expire_time => 5.minutes,
  :default_extension => '.yml',
  :perform_caching => true,
  :use_x_sendfile => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ResponseCache

Creates a ResponseCache object with the specified options.

Options are as follows:

:directory

the path to the temporary cache directory

:expire_time

the number of seconds a cached response is considered valid (defaults to 5 min)

:default_extension

the extension cached files should use (defaults to ‘.yml’)

:perform_caching

boolean value that turns caching on or off (defaults to true)

:logger

the application logging object

:use_x_sendfile

use X-Sendfile headers to speed up transfer of cached pages (not available on all web servers)



32
33
34
35
36
37
38
39
40
# File 'lib/serve/response_cache.rb', line 32

def initialize(options = {})
  options = options.symbolize_keys.reverse_merge(defaults)
  self.directory         = options[:directory]
  self.expire_time       = options[:expire_time]
  self.default_extension = options[:default_extension]
  self.perform_caching   = options[:perform_caching]
  self.logger            = options[:logger]
  self.use_x_sendfile    = options[:use_x_sendfile]
end

Instance Attribute Details

#default_extensionObject Also known as: page_cache_extension

Returns the value of attribute default_extension.



17
18
19
# File 'lib/serve/response_cache.rb', line 17

def default_extension
  @default_extension
end

#directoryObject Also known as: page_cache_directory

Returns the value of attribute directory.



17
18
19
# File 'lib/serve/response_cache.rb', line 17

def directory
  @directory
end

#expire_timeObject

Returns the value of attribute expire_time.



17
18
19
# File 'lib/serve/response_cache.rb', line 17

def expire_time
  @expire_time
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/serve/response_cache.rb', line 17

def logger
  @logger
end

#perform_cachingObject

Returns the value of attribute perform_caching.



17
18
19
# File 'lib/serve/response_cache.rb', line 17

def perform_caching
  @perform_caching
end

#use_x_sendfileObject

Returns the value of attribute use_x_sendfile.



17
18
19
# File 'lib/serve/response_cache.rb', line 17

def use_x_sendfile
  @use_x_sendfile
end

Instance Method Details

#cache_response(path, response) ⇒ Object

Caches a response object for path to disk.



43
44
45
46
47
48
49
# File 'lib/serve/response_cache.rb', line 43

def cache_response(path, response)
  if perform_caching
    path = clean(path)
    write_response(path, response)
  end
  response
end

#clearObject

Expires the entire cache.



87
88
89
90
91
# File 'lib/serve/response_cache.rb', line 87

def clear
  Dir["#{directory}/*"].each do |f|
    FileUtils.rm_rf f
  end
end

#expire_response(path) ⇒ Object

Expires the cached response for the specified path.



81
82
83
84
# File 'lib/serve/response_cache.rb', line 81

def expire_response(path)
  path = clean(path)
  expire_page(path)
end

#read_metadata(path) ⇒ Object

Returns metadata for path.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/serve/response_cache.rb', line 63

def (path)
  path = clean(path)
  name = "#{page_cache_path(path)}.yml"
  if File.exists?(name) and not File.directory?(name)
    content = File.open(name, "rb") { |f| f.read }
     = YAML::load(content)
     if ['expires'] >= Time.now
  end
rescue
  nil
end

#response_cached?(path) ⇒ Boolean

Returns true if a response is cached at the specified path.

Returns:

  • (Boolean)


76
77
78
# File 'lib/serve/response_cache.rb', line 76

def response_cached?(path)
  perform_caching && !!(path)
end

#update_response(path, response, request = nil) ⇒ Object

If perform_caching is set to true, updates a response object so that it mirrors the cached version. The request object is required to perform Last-Modified/If-Modified-Since checks–it is left optional to allow for backwards compatability.



54
55
56
57
58
59
60
# File 'lib/serve/response_cache.rb', line 54

def update_response(path, response, request=nil)
  if perform_caching
    path = clean(path)
    read_response(path, response, request)
  end
  response
end