Class: Tml::Cache
- Inherits:
-
Object
- Object
- Tml::Cache
- Defined in:
- lib/tml/cache.rb
Direct Known Subclasses
Tml::CacheAdapters::File, Tml::CacheAdapters::Memcache, Tml::CacheAdapters::Redis
Instance Method Summary collapse
-
#cache_name ⇒ Object
name of the cache adapter.
-
#clear(opts = {}) ⇒ Object
clears cache.
-
#default_cache_path ⇒ Object
default cache path.
-
#delete(key, opts = {}) ⇒ Object
deletes key from cache.
-
#download(cache_path = default_cache_path, version = nil) ⇒ Object
downloads cache from the CDN.
-
#enabled? ⇒ Boolean
checks if Tml is enabled.
-
#exist?(key, opts = {}) ⇒ Boolean
checks if the key exists.
- #extract_version(app, version = nil) ⇒ Object
-
#fetch(key, opts = {}) ⇒ Object
fetches key from cache.
-
#info(msg) ⇒ Object
logs information messages.
-
#namespace ⇒ Object
namespace of each cache key.
-
#read_only? ⇒ Boolean
by default all cache is read/write cache like files based should be set to read only.
-
#reset_version ⇒ Object
resets current version.
-
#store(key, data, opts = {}) ⇒ Object
stores key in cache.
-
#strip_extensions(data) ⇒ Object
remove extensions.
-
#upgrade_version ⇒ Object
upgrade current version.
-
#version ⇒ Object
version object.
-
#versioned_key(key, opts = {}) ⇒ Object
versioned name of cache key.
-
#warmup(version = nil) ⇒ Object
warmup cache.
-
#warn(msg) ⇒ Object
logs a warning.
Instance Method Details
#cache_name ⇒ Object
name of the cache adapter
82 83 84 |
# File 'lib/tml/cache.rb', line 82 def cache_name self.class.name.split('::').last end |
#clear(opts = {}) ⇒ Object
clears cache
129 130 131 |
# File 'lib/tml/cache.rb', line 129 def clear(opts = {}) # do nothing end |
#default_cache_path ⇒ Object
default cache path
180 181 182 183 184 185 186 187 188 |
# File 'lib/tml/cache.rb', line 180 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
119 120 121 |
# File 'lib/tml/cache.rb', line 119 def delete(key, opts = {}) # do nothing end |
#download(cache_path = default_cache_path, version = nil) ⇒ Object
downloads cache from the CDN
191 192 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 |
# File 'lib/tml/cache.rb', line 191 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
71 72 73 |
# File 'lib/tml/cache.rb', line 71 def enabled? Tml.config.cache_enabled? end |
#exist?(key, opts = {}) ⇒ Boolean
checks if the key exists
124 125 126 |
# File 'lib/tml/cache.rb', line 124 def exist?(key, opts = {}) false end |
#extract_version(app, version = nil) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/tml/cache.rb', line 133 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
108 109 110 111 |
# File 'lib/tml/cache.rb', line 108 def fetch(key, opts = {}) return nil unless block_given? yield end |
#info(msg) ⇒ Object
logs information messages
87 88 89 |
# File 'lib/tml/cache.rb', line 87 def info(msg) Tml.logger.info("#{cache_name} - #{msg}") end |
#namespace ⇒ Object
namespace of each cache key
97 98 99 100 |
# File 'lib/tml/cache.rb', line 97 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
77 78 79 |
# File 'lib/tml/cache.rb', line 77 def read_only? false end |
#reset_version ⇒ Object
resets current version
61 62 63 |
# File 'lib/tml/cache.rb', line 61 def reset_version version.reset end |
#store(key, data, opts = {}) ⇒ Object
stores key in cache
114 115 116 |
# File 'lib/tml/cache.rb', line 114 def store(key, data, opts = {}) # do nothing end |
#strip_extensions(data) ⇒ Object
remove extensions
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/tml/cache.rb', line 232 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 ⇒ Object
upgrade current version
66 67 68 |
# File 'lib/tml/cache.rb', line 66 def upgrade_version version.upgrade end |
#version ⇒ Object
version object
56 57 58 |
# File 'lib/tml/cache.rb', line 56 def version @version ||= Tml::CacheVersion.new(self) end |
#versioned_key(key, opts = {}) ⇒ Object
versioned name of cache key
103 104 105 |
# File 'lib/tml/cache.rb', line 103 def versioned_key(key, opts = {}) version.versioned_key(key, namespace) end |
#warmup(version = nil) ⇒ Object
warmup cache
149 150 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 |
# File 'lib/tml/cache.rb', line 149 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
92 93 94 |
# File 'lib/tml/cache.rb', line 92 def warn(msg) Tml.logger.warn("#{cache_name} - #{msg}") end |