Class: BuildpackSupport::Cache::CachedFile
- Inherits:
-
Object
- Object
- BuildpackSupport::Cache::CachedFile
- Defined in:
- lib/buildpack_support/cache/cached_file.rb
Overview
Represents a file cached on a filesystem
Note: this class is thread-safe, however access to the cached files is not
Instance Method Summary collapse
-
#cached(mode_enc, *additional_args) {|file, additional_args| ... } ⇒ Void
Opens the cached file.
-
#cached? ⇒ Boolean
Returns whether or not data is cached.
-
#destroy ⇒ Object
Destroys the cached file.
-
#etag(mode_enc, *additional_args) {|file| ... } ⇒ Void
Opens the etag file.
-
#etag? ⇒ Boolean
Returns whether or not an etag is stored.
-
#initialize(cache_root, uri) ⇒ CachedFile
constructor
Creates an instance of the cached file.
-
#last_modified(mode_enc, *additional_args) {|file| ... } ⇒ Void
Opens the last modified file.
-
#last_modified? ⇒ Boolean
Returns whether or not a last modified time stamp is stored.
Constructor Details
#initialize(cache_root, uri) ⇒ CachedFile
Creates an instance of the cached file. Files created and expected by this class will all be rooted at cache_root.
32 33 34 35 36 37 38 39 |
# File 'lib/buildpack_support/cache/cached_file.rb', line 32 def initialize(cache_root, uri) FileUtils.mkdir_p cache_root key = URI.escape(uri, '/') @cached = cache_root + "#{key}.cached" @etag = cache_root + "#{key}.etag" @last_modified = cache_root + "#{key}.last_modified" end |
Instance Method Details
#cached(mode_enc, *additional_args) {|file, additional_args| ... } ⇒ Void
Opens the cached file
48 49 50 |
# File 'lib/buildpack_support/cache/cached_file.rb', line 48 def cached(mode_enc, *additional_args, &block) @cached.open(mode_enc) { |f| block.call f, *additional_args } end |
#cached? ⇒ Boolean
Returns whether or not data is cached.
55 56 57 |
# File 'lib/buildpack_support/cache/cached_file.rb', line 55 def cached? @cached.exist? end |
#destroy ⇒ Object
Destroys the cached file
60 61 62 |
# File 'lib/buildpack_support/cache/cached_file.rb', line 60 def destroy [@cached, @etag, @last_modified].each { |f| f.delete if f.exist? } end |
#etag(mode_enc, *additional_args) {|file| ... } ⇒ Void
Opens the etag file
71 72 73 |
# File 'lib/buildpack_support/cache/cached_file.rb', line 71 def etag(mode_enc, *additional_args, &block) @etag.open(mode_enc) { |f| block.call f, *additional_args } end |
#etag? ⇒ Boolean
Returns whether or not an etag is stored.
78 79 80 |
# File 'lib/buildpack_support/cache/cached_file.rb', line 78 def etag? @etag.exist? end |
#last_modified(mode_enc, *additional_args) {|file| ... } ⇒ Void
Opens the last modified file
89 90 91 |
# File 'lib/buildpack_support/cache/cached_file.rb', line 89 def last_modified(mode_enc, *additional_args, &block) @last_modified.open(mode_enc) { |f| block.call f, *additional_args } end |
#last_modified? ⇒ Boolean
Returns whether or not a last modified time stamp is stored.
96 97 98 |
# File 'lib/buildpack_support/cache/cached_file.rb', line 96 def last_modified? @last_modified.exist? end |