class Tml::Cache

Public Instance Methods

cache_name() click to toggle source

name of the cache adapter

# File lib/tml/cache.rb, line 84
def cache_name
  self.class.name.split('::').last
end
clear(opts = {}) click to toggle source

clears cache

# File lib/tml/cache.rb, line 135
def clear(opts = {})
  # do nothing
end
default_cache_path() click to toggle source

default cache path

# File lib/tml/cache.rb, line 230
def default_cache_path
  @cache_path ||= begin
    path = Tml.config.cache[:path]
    path ||= 'config/tml'
    FileUtils.mkdir_p(path)
    FileUtils.chmod(0777, path)
    path
  end
end
delete(key, opts = {}) click to toggle source

deletes key from cache

# File lib/tml/cache.rb, line 125
def delete(key, opts = {})
  # do nothing
end
download(cache_path = default_cache_path, version = nil) click to toggle source

downloads cache from the CDN

# File lib/tml/cache.rb, line 241
def download(cache_path = default_cache_path, version = nil)
  t0 = Time.now
  Tml.logger = Logger.new(STDOUT)

  Tml.logger.debug('Starting cache download...')
  app = Tml::Application.new(key: Tml.config.application[:key], cdn_host: Tml.config.application[:cdn_host])
  extract_version(app, version)

  Tml.logger.debug("Downloading Version: #{Tml.cache.version}")

  archive_name = "#{Tml.cache.version}.tar.gz"
  path = "#{cache_path}/#{archive_name}"
  url = "#{app.cdn_host}/#{Tml.config.application[:key]}/#{archive_name}"

  Tml.logger.debug("Downloading cache file: #{url}")
  open(path, 'wb') do |file|
    file << open(url).read
  end

  Tml.logger.debug('Extracting cache file...')
  version_path = "#{cache_path}/#{Tml.cache.version}"
  Tml::Utils.untar(Tml::Utils.ungzip(File.new(path)), version_path)
  Tml.logger.debug("Cache has been stored in #{version_path}")

  File.unlink(path)

  begin
    current_path = 'current'
    FileUtils.chdir(cache_path)
    FileUtils.rm(current_path) if File.exist?(current_path)
    FileUtils.ln_s(Tml.cache.version.to_s, current_path)
    Tml.logger.debug("The new version #{Tml.cache.version} has been marked as current")
  rescue Exception => ex
    Tml.logger.debug("Could not generate current symlink to the cache path: #{ex.message}")
  end

  t1 = Time.now
  Tml.logger.debug("Cache download took #{t1-t0}s")
end
enabled?() click to toggle source

checks if Tml is enabled

# File lib/tml/cache.rb, line 73
def enabled?
  Tml.config.cache_enabled?
end
exist?(key, opts = {}) click to toggle source

checks if the key exists

# File lib/tml/cache.rb, line 130
def exist?(key, opts = {})
  false
end
extract_version(app, version = nil) click to toggle source

Pulls cache version from CDN

# File lib/tml/cache.rb, line 140
def extract_version(app, version = nil)
  if version
    Tml.cache.version.set(version.to_s)
  else
    version_data = app.api_client.get_from_cdn('version', {t: Time.now.to_i}, {uncompressed: true})

    unless version_data
      Tml.logger.debug('No releases have been generated yet. Please visit your Dashboard and publish translations.')
      return
    end

    Tml.cache.version.set(version_data['version'])
  end
end
fetch(key, opts = {}) { || ... } click to toggle source

fetches key from cache

# File lib/tml/cache.rb, line 114
def fetch(key, opts = {})
  return nil unless block_given?
  yield
end
info(msg) click to toggle source

logs information messages

# File lib/tml/cache.rb, line 89
def info(msg)
  Tml.logger.info("#{cache_name} - #{msg}")
end
namespace() click to toggle source

namespace of each cache key

# File lib/tml/cache.rb, line 103
def namespace
  return '#' if Tml.config.disabled?
  @namespace || Tml.config.cache[:namespace] || Tml.config.application[:key][0..5]
end
namespace=(value) click to toggle source
# File lib/tml/cache.rb, line 98
def namespace=(value)
  @namespace = value
end
read_only?() click to toggle source

by default all cache is read/write cache like files based should be set to read only

# File lib/tml/cache.rb, line 79
def read_only?
  false
end
reset_version() click to toggle source

resets current version

# File lib/tml/cache.rb, line 63
def reset_version
  version.reset
end
store(key, data, opts = {}) click to toggle source

stores key in cache

# File lib/tml/cache.rb, line 120
def store(key, data, opts = {})
  # do nothing
end
strip_extensions(data) click to toggle source

remove extensions

# File lib/tml/cache.rb, line 282
def strip_extensions(data)
  if data.is_a?(Hash)
    data = data.dup
    data.delete('extensions')
    return data
  end

  if data.is_a?(String) and data.match(/^\{/)
    data = JSON.parse(data)
    data.delete('extensions')
    data = data.to_json
  end

  data
end
upgrade_version() click to toggle source

upgrade current version

# File lib/tml/cache.rb, line 68
def upgrade_version
  version.upgrade
end
version() click to toggle source

version object

# File lib/tml/cache.rb, line 58
def version
  @version ||= Tml::CacheVersion.new(self)
end
versioned_key(key, opts = {}) click to toggle source

versioned name of cache key

# File lib/tml/cache.rb, line 109
def versioned_key(key, opts = {})
  version.versioned_key(key, namespace)
end
warmup(version = nil, cache_path = nil) click to toggle source

Warms up cache from CDN or local files

# File lib/tml/cache.rb, line 156
def warmup(version = nil, cache_path = nil)
  if cache_path.nil?
    warmup_from_cdn(version)
  else
    warmup_from_files(version, cache_path)
  end
end
warmup_from_cdn(version = nil) click to toggle source

Warms up cache from CDN

# File lib/tml/cache.rb, line 199
def warmup_from_cdn(version = nil)
  t0 = Time.now
  Tml.logger = Logger.new(STDOUT)

  Tml.logger.debug('Starting cache warmup from CDN...')
  app = Tml::Application.new(key: Tml.config.application[:key], cdn_host: Tml.config.application[:cdn_host])
  extract_version(app, version)

  Tml.logger.debug("Warming Up Version: #{Tml.cache.version}")

  application = app.api_client.get_from_cdn('application', {t: Time.now.to_i})
  Tml.cache.store(Tml::Application.cache_key, application)

  sources = app.api_client.get_from_cdn('sources', {t: Time.now.to_i}, {uncompressed: true})

  application['languages'].each do |lang|
    locale = lang['locale']
    language = app.api_client.get_from_cdn("#{locale}/language", {t: Time.now.to_i})
    Tml.cache.store(Tml::Language.cache_key(locale), language)

    sources.each do |src|
      source = app.api_client.get_from_cdn("#{locale}/sources/#{src}", {t: Time.now.to_i})
      Tml.cache.store(Tml::Source.cache_key(locale, src), source)
    end
  end

  t1 = Time.now
  Tml.logger.debug("Cache warmup took #{t1-t0}s")
end
warmup_from_files(version = nil, cache_path = nil) click to toggle source

Warms up cache from local files

# File lib/tml/cache.rb, line 165
def warmup_from_files(version = nil, cache_path = nil)
  t0 = Time.now
  Tml.logger = Logger.new(STDOUT)

  Tml.logger.debug('Starting cache warmup from local files...')
  version ||= Tml.config.cache[:version]
  cache_path ||= Tml.config.cache[:path]
  cache_path = "#{cache_path}/#{version}"

  Tml.cache.version.set(version.to_s)
  Tml.logger.debug("Warming Up Version: #{Tml.cache.version}")

  application = JSON.parse(File.read("#{cache_path}/application.json"))
  Tml.cache.store(Tml::Application.cache_key, application)

  sources = JSON.parse(File.read("#{cache_path}/sources.json"))

  application['languages'].each do |lang|
    locale = lang['locale']

    language = JSON.parse(File.read("#{cache_path}/#{locale}/language.json"))
    Tml.cache.store(Tml::Language.cache_key(locale), language)

    sources.each do |src|
      source = JSON.parse(File.read("#{cache_path}/#{locale}/sources/#{src}.json"))
      Tml.cache.store(Tml::Source.cache_key(locale, src), source)
    end
  end

  t1 = Time.now
  Tml.logger.debug("Cache warmup took #{t1-t0}s")
end
warn(msg) click to toggle source

logs a warning

# File lib/tml/cache.rb, line 94
def warn(msg)
  Tml.logger.warn("#{cache_name} - #{msg}")
end