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, mutable) ⇒ CachedFile

Creates an instance of the cached file. Files created and expected by this class will all be rooted at cache_root.

Parameters:

  • cache_root (Pathname)

    the filesystem root for the file created and expected by this class

  • uri (String)

    a uri which uniquely identifies the file in the cache

  • mutable (Boolean)

    whether the cached file should be mutable



33
34
35
36
37
38
39
40
41
# File 'lib/buildpack_support/cache/cached_file.rb', line 33

def initialize(cache_root, uri, mutable)
  key            = URI.escape(uri, ':/')
  @cached        = cache_root + "#{key}.cached"
  @etag          = cache_root + "#{key}.etag"
  @last_modified = cache_root + "#{key}.last_modified"
  @mutable       = mutable

  FileUtils.mkdir_p cache_root if mutable
end

Instance Method Details

#cached(mode_enc, *additional_args) {|file, additional_args| ... } ⇒ Void

Opens the cached file

Parameters:

  • mode_enc (String, integer)

    the mode to open the file in. Can be a string like “r” or an integer like File::CREAT | File::WRONLY.

  • additional_args (Array)

    any additional arguments to be passed to the block

Yields:

  • (file, additional_args)

    the cached file and any additional arguments passed in

Returns:

  • (Void)


50
51
52
# File 'lib/buildpack_support/cache/cached_file.rb', line 50

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.

Returns:

  • (Boolean)

    true if and only if data is cached



57
58
59
# File 'lib/buildpack_support/cache/cached_file.rb', line 57

def cached?
  @cached.exist?
end

#destroyObject

Destroys the cached file



62
63
64
# File 'lib/buildpack_support/cache/cached_file.rb', line 62

def destroy
  [@cached, @etag, @last_modified].each { |f| f.delete if f.exist? } if @mutable
end

#etag(mode_enc, *additional_args) {|file| ... } ⇒ Void

Opens the etag file

Parameters:

  • mode_enc (String, integer)

    the mode to open the file in. Can be a string like “r” or an integer like File::CREAT | File::WRONLY.

  • additional_args (Array)

    any additional arguments to be passed to the block

Yields:

  • (file)

    the etag file

Returns:

  • (Void)


73
74
75
# File 'lib/buildpack_support/cache/cached_file.rb', line 73

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.

Returns:

  • (Boolean)

    true if and only if an etag is stored



80
81
82
# File 'lib/buildpack_support/cache/cached_file.rb', line 80

def etag?
  @etag.exist?
end

#last_modified(mode_enc, *additional_args) {|file| ... } ⇒ Void

Opens the last modified file

Parameters:

  • mode_enc (String, integer)

    the mode to open the file in. Can be a string like “r” or an integer like File::CREAT | File::WRONLY.

  • additional_args (Array)

    any additional arguments to be passed to the block

Yields:

  • (file)

    the last modified file

Returns:

  • (Void)


91
92
93
# File 'lib/buildpack_support/cache/cached_file.rb', line 91

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.

Returns:

  • (Boolean)

    true if and only if a last modified time stamp is stored



98
99
100
# File 'lib/buildpack_support/cache/cached_file.rb', line 98

def last_modified?
  @last_modified.exist?
end