Class: Tml::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/tml/cache.rb

Instance Method Summary collapse

Instance Method Details

#cache_nameObject

name of the cache adapter



84
85
86
# File 'lib/tml/cache.rb', line 84

def cache_name
  self.class.name.split('::').last
end

#clear(opts = {}) ⇒ Object

clears cache



131
132
133
# File 'lib/tml/cache.rb', line 131

def clear(opts = {})
  # do nothing
end

#default_cache_pathObject

default cache path



182
183
184
185
186
187
188
189
190
# File 'lib/tml/cache.rb', line 182

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 = {}) ⇒ Object

deletes key from cache



121
122
123
# File 'lib/tml/cache.rb', line 121

def delete(key, opts = {})
  # do nothing
end

#download(cache_path = default_cache_path, version = nil) ⇒ Object

downloads cache from the CDN



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/tml/cache.rb', line 193

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])
  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?Boolean

checks if Tml is enabled

Returns:

  • (Boolean)


73
74
75
# File 'lib/tml/cache.rb', line 73

def enabled?
  Tml.config.cache_enabled?
end

#exist?(key, opts = {}) ⇒ Boolean

checks if the key exists

Returns:

  • (Boolean)


126
127
128
# File 'lib/tml/cache.rb', line 126

def exist?(key, opts = {})
  false
end

#extract_version(app, version = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/tml/cache.rb', line 135

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 = {}) ⇒ Object

fetches key from cache



110
111
112
113
# File 'lib/tml/cache.rb', line 110

def fetch(key, opts = {})
  return nil unless block_given?
  yield
end

#info(msg) ⇒ Object

logs information messages



89
90
91
# File 'lib/tml/cache.rb', line 89

def info(msg)
  Tml.logger.info("#{cache_name} - #{msg}")
end

#namespaceObject

namespace of each cache key



99
100
101
102
# File 'lib/tml/cache.rb', line 99

def namespace
  return '#' if Tml.config.disabled?
  Tml.config.cache[:namespace] || Tml.config.application[:key][0..5]
end

#read_only?Boolean

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

Returns:

  • (Boolean)


79
80
81
# File 'lib/tml/cache.rb', line 79

def read_only?
  false
end

#reset_versionObject

resets current version



63
64
65
# File 'lib/tml/cache.rb', line 63

def reset_version
  version.reset
end

#store(key, data, opts = {}) ⇒ Object

stores key in cache



116
117
118
# File 'lib/tml/cache.rb', line 116

def store(key, data, opts = {})
  # do nothing
end

#strip_extensions(data) ⇒ Object

remove extensions



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/tml/cache.rb', line 234

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_versionObject

upgrade current version



68
69
70
# File 'lib/tml/cache.rb', line 68

def upgrade_version
  version.upgrade
end

#versionObject

version object



58
59
60
# File 'lib/tml/cache.rb', line 58

def version
  @version ||= Tml::CacheVersion.new(self)
end

#versioned_key(key, opts = {}) ⇒ Object

versioned name of cache key



105
106
107
# File 'lib/tml/cache.rb', line 105

def versioned_key(key, opts = {})
  version.versioned_key(key, namespace)
end

#warmup(version = nil) ⇒ Object

warmup cache



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/tml/cache.rb', line 151

def warmup(version = nil)
  t0 = Time.now
  Tml.logger = Logger.new(STDOUT)

  Tml.logger.debug('Starting cache warmup...')
  app = Tml::Application.new(key: Tml.config.application[:key])
  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

#warn(msg) ⇒ Object

logs a warning



94
95
96
# File 'lib/tml/cache.rb', line 94

def warn(msg)
  Tml.logger.warn("#{cache_name} - #{msg}")
end