Skip to content

Commit

Permalink
Merge 71928434-mkdir-immutable-cache to master
Browse files Browse the repository at this point in the history
[Completes #71928434]
  • Loading branch information
nebhale committed May 23, 2014
2 parents 26bc064 + 6606dd9 commit 876f77a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lib/java_buildpack/util/cache/cached_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ class CachedFile
#
# @param [Pathname] cache_root the filesystem root for the file created and expected by this class
# @param [String] uri a uri which uniquely identifies the file in the cache
def initialize(cache_root, uri)
FileUtils.mkdir_p cache_root

# @param [Boolean] mutable whether the cached file should be mutable
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

# Opens the cached file
Expand All @@ -60,7 +62,7 @@ def cached?

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

# Opens the etag file
Expand Down
6 changes: 3 additions & 3 deletions lib/java_buildpack/util/cache/download_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get(uri, &block)
# @param [String] uri the URI of the item
# @return [Void]
def evict(uri)
CachedFile.new(@mutable_cache_root, uri).destroy
CachedFile.new(@mutable_cache_root, uri, true).destroy
end

private
Expand Down Expand Up @@ -175,7 +175,7 @@ def cache_last_modified(response, cached_file)
end

def from_mutable_cache(uri)
cached_file = CachedFile.new(@mutable_cache_root, uri)
cached_file = CachedFile.new @mutable_cache_root, uri, true
cached = update URI(uri), cached_file
[cached_file, cached]
rescue => e
Expand All @@ -185,7 +185,7 @@ def from_mutable_cache(uri)

def from_immutable_caches(uri)
@immutable_cache_roots.each do |cache_root|
candidate = CachedFile.new cache_root, uri
candidate = CachedFile.new cache_root, uri, false

next unless candidate.cached?

Expand Down
26 changes: 25 additions & 1 deletion spec/java_buildpack/util/cache/cached_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,30 @@
describe JavaBuildpack::Util::Cache::CachedFile do
include_context 'application_helper'

let(:file_cache) { described_class.new(app_dir, 'http://foo-uri/') }
let(:cache_root) { app_dir + 'cache/root' }

let(:file_cache) { described_class.new(app_dir, 'http://foo-uri/', true) }

it 'should not create any files on initialization' do
%w(cached etag last_modified).each { |extension| expect(cache_file(extension)).not_to exist }
end

it 'should create cache_root if mutable' do
expect(cache_root).not_to exist

described_class.new(cache_root, 'http://foo-uri/', true)

expect(cache_root).to exist
end

it 'should not create cache_root if immutable' do
expect(cache_root).not_to exist

described_class.new(cache_root, 'http://foo-uri/', false)

expect(cache_root).not_to exist
end

it 'should not detect cached file' do
expect(file_cache.cached?).not_to be
end
Expand Down Expand Up @@ -62,6 +80,12 @@
%w(cached etag last_modified).each { |extension| expect(cache_file(extension)).not_to exist }
end

it 'should not destroy all files if immutable' do
described_class.new(app_dir, 'http://foo-uri/', false).destroy

%w(cached etag last_modified).each { |extension| expect(cache_file(extension)).to exist }
end

it 'should call the block with the content of the etag file' do
expect { |b| file_cache.etag(File::RDONLY, 'test-arg', &b) }.to yield_file_with_content(/foo-etag/)
end
Expand Down

0 comments on commit 876f77a

Please sign in to comment.