class CopyTunerClient::Cache

Responsible for locking down access to data used by both threads.
this class directly.
like a Hash. Applications using the client will not need to interact with
Manages synchronization of copy between {I18nBackend} and {Client}. Acts

def [](key)

Returns:
  • (String) - the contents of the blurb

Parameters:
  • key (String) -- the key of the desired blurb
def [](key)
  lock { @blurbs[key] }
end

def []=(key, value)

Parameters:
  • value (String) -- the new contents of the blurb
  • key (String) -- the key of the blurb to update
def []=(key, value)
  return if key =~ @exclude_key_regexp
  lock { @queued[key] = value }
end

def download

def download
  @started = true
  res = client.download do |downloaded_blurbs|
    downloaded_blurbs.reject! { |key, value| value == '' }
    lock { @blurbs = downloaded_blurbs }
  end
  @last_downloaded_at = Time.now.utc
  res
rescue ConnectionError => error
  logger.error error.message
ensure
  @downloaded = true
end

def export

Returns:
  • (String) - yaml
def export
  keys = {}
  lock do
    @blurbs.sort.each do |(blurb_key, value)|
      current = keys
      yaml_keys = blurb_key.split('.')
      0.upto(yaml_keys.size - 2) do |i|
        key = yaml_keys[i]
        # Overwrite en.key with en.sub.key
        unless current[key].class == Hash
          current[key] = {}
        end
        current = current[key]
      end
      current[yaml_keys.last] = value
    end
  end
  unless keys.size < 1
    keys.to_yaml
  end
end

def flush

def flush
  res = with_queued_changes do |queued|
    client.upload queued
  end
  @last_uploaded_at = Time.now.utc
  res
rescue ConnectionError => error
  logger.error error.message
end

def initialize(client, options)

Options Hash: (**options)
  • :logger (Logger) -- where errors should be logged

Parameters:
  • options (Hash) --
  • client (Client) -- the client used to fetch and upload data
def initialize(client, options)
  @blurbs = {}
  @client = client
  @downloaded = false
  @logger = options[:logger]
  @mutex = Mutex.new
  @queued = {}
  @started = false
  @exclude_key_regexp = options[:exclude_key_regexp]
end

def keys

Returns:
  • (Array) - keys
def keys
  lock { @blurbs.keys }
end

def lock(&block)

def lock(&block)
  @mutex.synchronize &block
end

def pending?

def pending?
  @started && !@downloaded
end

def sync

Downloads and then flushes
def sync
  download
  flush
end

def wait_for_download

Waits until the first download has finished.
def wait_for_download
  if pending?
    logger.info 'Waiting for first download'
    if logger.respond_to? :flush
      logger.flush
    end
    while pending?
      sleep 0.1
    end
  end
end

def with_queued_changes

def with_queued_changes
  changes_to_push = nil
  lock do
    unless @queued.empty?
      changes_to_push = @queued
      @queued = {}
    end
  end
  if changes_to_push
    yield changes_to_push
  end
end