class S3Repo::Cache

Cache object, stores S3 objects on disk

Constants

TMPDIRS

Public Class Methods

new(params = {}) click to toggle source
Calls superclass method S3Repo::Base::new
# File lib/s3repo/cache.rb, line 15
def initialize(params = {})
  super
  [partialdir, cachedir].each { |x| FileUtils.mkdir_p x }
end

Public Instance Methods

download(key, refresh = true) click to toggle source
# File lib/s3repo/cache.rb, line 20
def download(key, refresh = true)
  path = expand_path key
  get_object(key, path) if refresh || !cached?(path)
  path
end

Private Instance Methods

atomic_get_object(key, path) click to toggle source
# File lib/s3repo/cache.rb, line 48
def atomic_get_object(key, path)
  tmpfile = Tempfile.create(key, partialdir)
  object = client.get_object(
    key: key, if_none_match: etags[key], response_target: tmpfile
  )
  tmpfile.close
  File.rename tmpfile.path, path
  object
end
cached?(path) click to toggle source
# File lib/s3repo/cache.rb, line 36
def cached?(path)
  File.exist? path
end
cachedir() click to toggle source
# File lib/s3repo/cache.rb, line 62
def cachedir
  File.join(tmpdir, 'cache')
end
etags() click to toggle source
# File lib/s3repo/cache.rb, line 58
def etags
  @etags ||= {}
end
expand_path(key) click to toggle source
# File lib/s3repo/cache.rb, line 32
def expand_path(key)
  File.absolute_path(key, cachedir)
end
file_cache() click to toggle source
# File lib/s3repo/cache.rb, line 28
def file_cache
  raise('Tried to call file_cache recursively')
end
get_object(key, path) click to toggle source
# File lib/s3repo/cache.rb, line 40
def get_object(key, path)
  FileUtils.mkdir_p File.dirname(path)
  object = atomic_get_object(key, path)
  etags[key] = object.etag
rescue Aws::S3::Errors::NotModified
  return
end
partialdir() click to toggle source
# File lib/s3repo/cache.rb, line 66
def partialdir
  File.join(tmpdir, 'partial')
end
tmpdir() click to toggle source
# File lib/s3repo/cache.rb, line 70
def tmpdir
  @tmpdir ||= Dir.mktmpdir 's3repo', tmpdir_root
end
tmpdir_root() click to toggle source
# File lib/s3repo/cache.rb, line 74
def tmpdir_root
  @tmpdir_root ||= File.absolute_path(
    @options[:tmpdir] || TMPDIRS.compact.first
  )
end