Module: CacheHelper
- Defined in:
- lib/utils/cache_helper.rb
Overview
Module CacheHelper
Class Method Summary collapse
-
.clear_cache(type = '.json') ⇒ Object
The method clear all application cache.
-
.load_cached(key, params = {}) { ... } ⇒ Hash
The method helps to load cached json.
-
.load_knowledge(key) ⇒ Object
The method helps to get build in knowledge by key.
-
.update_knowledge(key) { ... } ⇒ Object
The method helps to update knowledge by key in yield block.
Class Method Details
.clear_cache(type = '.json') ⇒ Object
The method clear all application cache
10 11 12 13 14 15 |
# File 'lib/utils/cache_helper.rb', line 10 def self.clear_cache(type = '.json') dir = "#{Dir.tmpdir}/#{BrowserWebData::TMP_DIR}/*#{type}" Dir.glob(dir).each { |path| FileUtils.rm_f(path) } end |
.load_cached(key, params = {}) { ... } ⇒ Hash
The method helps to load cached json. This cache is permanent and reload only if no exist or set demand_reload
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/utils/cache_helper.rb', line 30 def self.load_cached(key, params = {}, &block) default_load_attrs = { update: false, json: {symbolize_names: true}, ttl: 0, demanded_reload: false } params = default_load_attrs.merge(params) hash = {} cache_dir_path = "#{Dir.tmpdir}/#{BrowserWebData::TMP_DIR}" Dir.mkdir(cache_dir_path) unless Dir.exist?(cache_dir_path) cache_file_path = "#{cache_dir_path}/#{StringHelper.get_clear_file_path(key)}.json" if params[:demanded_reload] || !File.exists?(cache_file_path) || (params[:ttl] && Time.now - File.ctime(cache_file_path) > params[:ttl]) if block_given? hash = yield hash File.open(cache_file_path, 'w') { |f| f.puts hash.to_json } unless hash.empty? end else hash = JSON.parse(File.read(cache_file_path).force_encoding('UTF-8'), params[:json]) end HashHelper.recursive_symbolize_keys(hash) end |
.load_knowledge(key) ⇒ Object
The method helps to get build in knowledge by key.
89 90 91 92 93 94 |
# File 'lib/utils/cache_helper.rb', line 89 def self.load_knowledge(key) dir_path = "#{File.dirname(File.expand_path('..', __FILE__))}/knowledge" file_path = "#{dir_path}/#{StringHelper.get_clear_file_path(key)}.json" JSON.parse(File.read(file_path), symbolize_names: true) end |
.update_knowledge(key) { ... } ⇒ Object
The method helps to update knowledge by key in yield block.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/utils/cache_helper.rb', line 65 def self.update_knowledge(key) dir_path = "#{File.dirname(File.expand_path('..', __FILE__))}/knowledge" file_path = "#{dir_path}/#{StringHelper.get_clear_file_path(key)}.json" hash = {} if !File.exists?(file_path) if block_given? hash = yield hash File.open(file_path, 'w') { |f| f.puts hash.to_json } unless hash.empty? end else old_hash = JSON.parse(File.read(file_path).force_encoding('UTF-8'), symbolize_names: true) hash = yield old_hash File.open(file_path, 'w') { |f| f.puts hash.to_json } unless hash.empty? end HashHelper.recursive_symbolize_keys(hash) end |