Class: BuildpackSupport::Cache::DownloadCache
- Inherits:
-
Object
- Object
- BuildpackSupport::Cache::DownloadCache
- 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
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
-
#evict(uri) ⇒ Void
Removes an item from the mutable cache.
-
#get(uri) {|file, downloaded| ... } ⇒ Void
Retrieves an item from the cache.
-
#initialize(mutable_cache_root = Pathname.new(Dir.tmpdir), *immutable_cache_roots) ⇒ DownloadCache
constructor
Creates an instance of the cache that is backed by a number of filesystem locations.
Methods included from DirectoryFinder
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.
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.
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.
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 |