Class: BuildpackSupport::Cache::CachedFile

Inherits:
Object
  • Object
show all
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

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

Yields:

  • (file, additional_args)

    the cached file and any additional arguments passed in



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

#destroyObject

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

Yields:

  • (file)

    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

Yields:

  • (file)

    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