Class: Serve::ResponseCache
- Inherits:
-
Object
- Object
- Serve::ResponseCache
- 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
-
#default_extension ⇒ Object
(also: #page_cache_extension)
Returns the value of attribute default_extension.
-
#directory ⇒ Object
(also: #page_cache_directory)
Returns the value of attribute directory.
-
#expire_time ⇒ Object
Returns the value of attribute expire_time.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#perform_caching ⇒ Object
Returns the value of attribute perform_caching.
-
#use_x_sendfile ⇒ Object
Returns the value of attribute use_x_sendfile.
Instance Method Summary collapse
-
#cache_response(path, response) ⇒ Object
Caches a response object for path to disk.
-
#clear ⇒ Object
Expires the entire cache.
-
#expire_response(path) ⇒ Object
Expires the cached response for the specified path.
-
#initialize(options = {}) ⇒ ResponseCache
constructor
Creates a ResponseCache object with the specified options.
-
#read_metadata(path) ⇒ Object
Returns metadata for path.
-
#response_cached?(path) ⇒ Boolean
Returns true if a response is cached at the specified path.
-
#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.
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( = {}) = .symbolize_keys.reverse_merge(defaults) self.directory = [:directory] self.expire_time = [:expire_time] self.default_extension = [:default_extension] self.perform_caching = [:perform_caching] self.logger = [:logger] self.use_x_sendfile = [:use_x_sendfile] end |
Instance Attribute Details
#default_extension ⇒ Object 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 |
#directory ⇒ Object 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_time ⇒ Object
Returns the value of attribute expire_time.
17 18 19 |
# File 'lib/serve/response_cache.rb', line 17 def expire_time @expire_time end |
#logger ⇒ Object
Returns the value of attribute logger.
17 18 19 |
# File 'lib/serve/response_cache.rb', line 17 def logger @logger end |
#perform_caching ⇒ Object
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_sendfile ⇒ Object
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 |
#clear ⇒ Object
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.
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 |