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.
- #namespace=(value) ⇒ Object
-
#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
84 85 86 |
# File 'lib/tml/cache.rb', line 84 def cache_name self.class.name.split('::').last end |
#clear(opts = {}) ⇒ Object
clears cache
135 136 137 |
# File 'lib/tml/cache.rb', line 135 def clear(opts = {}) # do nothing end |
#default_cache_path ⇒ Object
default cache path
186 187 188 189 190 191 192 193 194 |
# File 'lib/tml/cache.rb', line 186 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
125 126 127 |
# File 'lib/tml/cache.rb', line 125 def delete(key, opts = {}) # do nothing end |
#download(cache_path = default_cache_path, version = nil) ⇒ Object
downloads cache from the CDN
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 232 233 234 235 |
# File 'lib/tml/cache.rb', line 197 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.}") end t1 = Time.now Tml.logger.debug("Cache download took #{t1-t0}s") end |
#enabled? ⇒ Boolean
checks if Tml is enabled
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
130 131 132 |
# File 'lib/tml/cache.rb', line 130 def exist?(key, opts = {}) false end |
#extract_version(app, version = nil) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/tml/cache.rb', line 139 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
114 115 116 117 |
# File 'lib/tml/cache.rb', line 114 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 |
#namespace ⇒ Object
namespace of each cache key
103 104 105 106 |
# 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) ⇒ Object
98 99 100 |
# File 'lib/tml/cache.rb', line 98 def namespace=(value) @namespace = value end |
#read_only? ⇒ Boolean
by default all cache is read/write cache like files based should be set to read only
79 80 81 |
# File 'lib/tml/cache.rb', line 79 def read_only? false end |
#reset_version ⇒ Object
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
120 121 122 |
# File 'lib/tml/cache.rb', line 120 def store(key, data, opts = {}) # do nothing end |
#strip_extensions(data) ⇒ Object
remove extensions
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/tml/cache.rb', line 238 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
68 69 70 |
# File 'lib/tml/cache.rb', line 68 def upgrade_version version.upgrade end |
#version ⇒ Object
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
109 110 111 |
# File 'lib/tml/cache.rb', line 109 def versioned_key(key, opts = {}) version.versioned_key(key, namespace) end |
#warmup(version = nil) ⇒ Object
warmup cache
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 180 181 182 183 |
# File 'lib/tml/cache.rb', line 155 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], 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 |