class Dragonfly::Cache::Manager

Constants

MAX_SHA_SIZE
MIN_SHA_SIZE

Attributes

map[R]
plugin[R]
storage[R]

Public Class Methods

new(plugin) click to toggle source
# File lib/dragonfly/cache/manager.rb, line 18
def initialize(plugin)
  @plugin = plugin
  @map = Dragonfly::Cache::Mapper::Yaml.new(config.servers_options)
  @storage = Dragonfly::Cache::Storage::Local.new(config.servers_options)
end

Public Instance Methods

cache(job) { || ... } click to toggle source
# File lib/dragonfly/cache/manager.rb, line 24
def cache(job)
  return @map[job.sha] if @map.key?(job.sha)

  store(job, yield)
end
job_options(job) click to toggle source
# File lib/dragonfly/cache/manager.rb, line 37
def job_options(job)
  {
    shaish: shaish(job),
    normalized_name: normalized_name(job)
  }
end
valid?(job, uri) click to toggle source
# File lib/dragonfly/cache/manager.rb, line 30
def valid?(job, uri)
  valid = (@map.key?(job.sha) && @map[job.sha] == uri) || !@map.key?(job.sha)
  valid &= (@map.value?(uri) && @map.key(uri) == job.sha) || !@map.value?(uri)
  increase_sha_size! unless valid
  valid
end

Protected Instance Methods

increase_sha_size!() click to toggle source
# File lib/dragonfly/cache/manager.rb, line 62
def increase_sha_size!
  raise Error, "Can't build longer :sha identifier" if @sha_size == MAX_SHA_SIZE

  @sha_size += 1
end
normalized_name(job) click to toggle source
# File lib/dragonfly/cache/manager.rb, line 50
def normalized_name(job)
  basename = job.basename || job.signature
  sanitized = basename.gsub(/[[:space:][:punct:][:cntrl:]]/, ' ').squeeze(' ').strip
  transliterated = I18n.transliterate(sanitized, replacement: ' ').squeeze(' ').strip
  downcased = (transliterated.empty? ? job.signature : transliterated).downcase.tr(' ', '-')
  [downcased, job.ext].compact.join('.')
end
sha_size() click to toggle source
# File lib/dragonfly/cache/manager.rb, line 58
def sha_size
  @sha_size ||= [[storage.sha_size, MIN_SHA_SIZE].compact.max, MAX_SHA_SIZE].min
end
shaish(job) click to toggle source
# File lib/dragonfly/cache/manager.rb, line 46
def shaish(job)
  job.sha[0..(sha_size - 1)]
end
store(job, url) click to toggle source
# File lib/dragonfly/cache/manager.rb, line 68
def store(job, url)
  storage.store(url, job)
  map.store(job.sha, url)
  url
rescue Dragonfly::Cache::Error => e
  Dragonfly.warn(e.message)
end