Class: BuildpackSupport::Cache::DownloadCache

Inherits:
Object
  • Object
show all
Extended by:
DirectoryFinder
Defined in:
lib/buildpack_support/cache/download_cache.rb

Overview

A cache for downloaded files that is configured to use a filesystem as the backing store.

Note: this class is thread-safe, however access to the cached files is not

References:

Direct Known Subclasses

ApplicationCache

Constant Summary collapse

CACHED_RESOURCES_DIRECTORY =

The location to find cached resources in the buildpack

DownloadCache.load_path_peer('resources/cache').freeze

Instance Method Summary collapse

Methods included from DirectoryFinder

load_path_peer

Constructor Details

#initialize(mutable_cache_root = Pathname.new(Dir.tmpdir), *immutable_cache_roots) ⇒ DownloadCache

Creates an instance of the cache that is backed by a number of filesystem locations. The first argument (mutable_cache_root) is the only location that downloaded files will be stored in.

Parameters:

  • mutable_cache_root (Pathname) (defaults to: Pathname.new(Dir.tmpdir))

    the filesystem location in which find cached files in. This will also be the location that all downloaded files are written to.

  • immutable_cache_roots (Pathname)

    other filesystem locations to find cached files in. No files will be written to these locations.



50
51
52
53
54
# File 'lib/buildpack_support/cache/download_cache.rb', line 50

def initialize(mutable_cache_root = Pathname.new(Dir.tmpdir), *immutable_cache_roots)
  @logger                = BuildpackSupport::Logging::LoggerFactory.instance.get_logger DownloadCache
  @mutable_cache_root    = mutable_cache_root
  @immutable_cache_roots = immutable_cache_roots.unshift mutable_cache_root
end

Instance Method Details

#evict(uri) ⇒ Void

Removes an item from the mutable cache.

Parameters:

  • uri (String)

    the URI of the item

Returns:

  • (Void)


76
77
78
# File 'lib/buildpack_support/cache/download_cache.rb', line 76

def evict(uri)
  CachedFile.new(@mutable_cache_root, uri, true).destroy
end

#get(uri) {|file, downloaded| ... } ⇒ Void

Retrieves an item from the cache. Yields an open file containing the item’s content or raises an exception if the item cannot be retrieved.

Parameters:

  • uri (String)

    the URI of the item

Yields:

  • (file, downloaded)

    the file representing the cached item and whether the file was downloaded or was already in the cache

Returns:

  • (Void)


63
64
65
66
67
68
69
70
# File 'lib/buildpack_support/cache/download_cache.rb', line 63

def get(uri, &block)
  cached_file, downloaded = nil, nil
  cached_file, downloaded = from_mutable_cache uri if InternetAvailability.instance.available?
  cached_file, downloaded = from_immutable_caches(uri), false unless cached_file

  fail "Unable to find cached file for #{uri}" unless cached_file
  cached_file.cached(File::RDONLY | File::BINARY, downloaded, &block)
end