Class: Chef::Provider::RemoteFile::CacheControlData
- Inherits:
-
Object
- Object
- Chef::Provider::RemoteFile::CacheControlData
- Defined in:
- lib/chef/provider/remote_file/cache_control_data.rb
Overview
== CacheControlData Implements per-uri storage of cache control data for a remote resource along with a sanity check checksum of the file in question. Provider::RemoteFile protocol implementation classes can use this information to avoid re-fetching files when the current copy is up to date. The way this information is used is protocol-dependent. For HTTP, this information is sent to the origin server via headers to make a conditional GET request.
== API
The general shape of the API is active-record-the-pattern-like. New
instances should be instantiated via
CacheControlData.load_and_validate
, which will do a find-or-create
operation and then sanity check the data against the checksum of the
current copy of the file. If there is no data or the sanity check
fails, the etag
and mtime
attributes will be set to nil; otherwise
they are populated with the previously saved values.
After fetching a file, the CacheControlData instance should be updated with new etag, mtime and checksum values in whatever format is preferred by the protocol used. Then call #save to save the data to disk.
Instance Attribute Summary collapse
-
#checksum ⇒ Object
SHA2-256 Hash of the file as last fetched.
-
#etag ⇒ Object
Entity Tag of the resource.
-
#mtime ⇒ Object
Last modified time of the remote resource.
-
#uri ⇒ Object
readonly
URI of the resource as a String.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(uri) ⇒ CacheControlData
constructor
A new instance of CacheControlData.
-
#json_data ⇒ Object
:nodoc: JSON representation of this object for storage.
- #load ⇒ Object
-
#save ⇒ Object
Saves the data to disk using Chef::FileCache.
- #validate!(current_copy_checksum) ⇒ Object
Constructor Details
#initialize(uri) ⇒ CacheControlData
Returns a new instance of CacheControlData.
83 84 85 86 87 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 83 def initialize(uri) uri = uri.dup uri.password = "XXXX" unless uri.userinfo.nil? @uri = uri.to_s end |
Instance Attribute Details
#checksum ⇒ Object
SHA2-256 Hash of the file as last fetched.
77 78 79 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 77 def checksum @checksum end |
#etag ⇒ Object
Entity Tag of the resource. HTTP-specific. See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.2 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19
65 66 67 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 65 def etag @etag end |
#mtime ⇒ Object
Last modified time of the remote resource. Different protocols will use different types for this field (e.g., string representation of a specific date format, integer, etc.) For HTTP-specific references, see:
74 75 76 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 74 def mtime @mtime end |
#uri ⇒ Object (readonly)
URI of the resource as a String. This is the "primary key" used for storage and retrieval.
81 82 83 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 81 def uri @uri end |
Class Method Details
.load_and_validate(uri, current_copy_checksum) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 55 def self.load_and_validate(uri, current_copy_checksum) ccdata = new(uri) ccdata.load ccdata.validate!(current_copy_checksum) ccdata end |
Instance Method Details
#json_data ⇒ Object
:nodoc: JSON representation of this object for storage.
117 118 119 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 117 def json_data Chef::JSONCompat.to_json(hash_data) end |
#load ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 89 def load if previous_cc_data = load_data apply(previous_cc_data) self else false end end |
#save ⇒ Object
Saves the data to disk using Chef::FileCache. The filename is a sanitized version of the URI with a MD5 of the same URI appended (to avoid collisions between different URIs having the same sanitized form).
111 112 113 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 111 def save Chef::FileCache.store("remote_file/#{sanitized_cache_file_basename}", json_data) end |
#validate!(current_copy_checksum) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/chef/provider/remote_file/cache_control_data.rb', line 98 def validate!(current_copy_checksum) if current_copy_checksum.nil? || checksum != current_copy_checksum reset! false else true end end |