marfa 0.4.7 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16f4e65b65810ba46b1595a5e3f4e83e32dc6c88
4
- data.tar.gz: c42bcbe1d65ef595ec533085c94a46450b67e71b
3
+ metadata.gz: c0759f81941c873aa1735687444f031c919ea122
4
+ data.tar.gz: 5a395cc867d247b1524bc8447b168296fdb8065a
5
5
  SHA512:
6
- metadata.gz: e6e590b243253f6e752e511f5b238f7562bfe55dd4dd82fb88c4f7bc6220847319e0964c3d378e6eb88793a42071295dcbb76380cd78c535e2a7e9262e46053b
7
- data.tar.gz: 9bc025d0a62bcbd857bea747feb7d2622e85e11f115f5dfb05ce5353c4d7e53f3345639b853f261c0d7bd0269a0949678906eec7401da04bf37bd7227bb3f491
6
+ metadata.gz: 8f396b685edab492541f969c49cc10e83ae1e9a5685c1c7addee2cd87a311f0c8aa16c9594b3bc44c89e5c36c68bb8d87284c8209bc18a940c397c20c68f1196
7
+ data.tar.gz: a00519de9f1b9ed6c32c04e3e61391585a6b2c22ed75306750e702d058abc18adef2205cc7850ad3e4df1058e67f35fc0b04a453109d7dbe2d88b00dda2db14c
data/lib/marfa/cache.rb CHANGED
@@ -14,6 +14,7 @@ module Marfa
14
14
  # Marfa.cache.set('key', 'value', 7200)
15
15
  def set(key, value, _time = nil)
16
16
  return unless @config[:enabled]
17
+ return if _time.zero?
17
18
  if _time.is_a? Numeric
18
19
  @redis.set(key, value, ex: _time) # ex - is seconds
19
20
  else
@@ -167,6 +167,36 @@ module Marfa
167
167
  haml :"#{template}", locals: data
168
168
  end
169
169
 
170
+ # Render block with data from cache, return html
171
+ # @param options [Hash] - options hash
172
+ # @example
173
+ # render_block_with_data({ path: 'index/index', tags: ['tag1', 'tag2'] })
174
+ # @return [String] rendered block
175
+ def render_block_with_data(options)
176
+ # TODO: Improve caching with parameters
177
+ cache_time = options[:cache_time] || Marfa.config.cache[:expiration_time]
178
+ tags = options[:tags] || []
179
+
180
+ kind = 'block'
181
+ kind += "-#{@device}" if Marfa.config.cache[:use_device]
182
+ tags += query_to_tags(options[:query])
183
+
184
+ if cache_time.positive?
185
+ content = get_cached_content(kind, options[:path], tags)
186
+ return content unless content.nil?
187
+ end
188
+
189
+ data = options[:data]
190
+ data = data.merge(options[:locals]) unless options[:locals].nil?
191
+
192
+ full_path = Marfa.config.block_templates_path + '/' + options[:path]
193
+
194
+ return render_content(full_path, data) if cache_time.zero?
195
+
196
+ cache_key = Marfa.cache.create_key(kind, options[:path], tags)
197
+ render_cached_content(cache_key, full_path, data)
198
+ end
199
+
170
200
  alias_method :render_component, :render_block
171
201
  alias_method :render_static_component, :render_static_block
172
202
  end
@@ -37,10 +37,16 @@ module Marfa
37
37
  def self.get_raw_data(params)
38
38
  result = {}
39
39
  path = params[:path]
40
+ cache_key = "data_#{path}#{params[:query]}".scan(/\w+/).join('_')
40
41
 
41
42
  begin
42
- response = RestClient.get("#{Marfa.config.api_server}#{path}", { params: params[:query], headers: {} })
43
- result = JSON.parse(response.body, symbolize_names: true)
43
+ if Marfa.cache.exist?(cache_key)
44
+ result = JSON.parse(Marfa.cache.get(cache_key), symbolize_names: true)
45
+ else
46
+ response = RestClient.get("#{Marfa.config.api_server}#{path}", { params: params[:query], headers: {} })
47
+ Marfa.cache.set(cache_key, response.body, params[:cache_time] || 7200)
48
+ result = JSON.parse(response.body, symbolize_names: true)
49
+ end
44
50
  rescue => exception
45
51
  if [:development, :test].include? Marfa.config.environment
46
52
  _log_exception(exception, path, params)
@@ -56,17 +62,33 @@ module Marfa
56
62
  def self.get_raw_data_with_pagination(params)
57
63
  result = {}
58
64
  path = params[:path]
65
+ cache_key = "data_with_pagination_#{path}#{params[:query]}".scan(/\w+/).join('_')
59
66
 
60
67
  begin
61
- response = RestClient.get("#{Marfa.config.api_server}#{path}", { params: params[:query], headers: {} })
62
- result[:data] = JSON.parse(response.body, symbolize_names: true)
63
- result[:data_count] = response.headers[:x_count].to_i unless response.headers[:x_count].nil?
64
- result[:data_pages] = response.headers[:x_pages].to_i unless response.headers[:x_pages].nil?
68
+ if Marfa.cache.exist?(cache_key)
69
+ result = JSON.parse(Marfa.cache.get(cache_key), symbolize_names: true)
70
+ else
71
+ response = RestClient.get("#{Marfa.config.api_server}#{path}", { params: params[:query], headers: {} })
72
+ result[:data] = JSON.parse(response.body, symbolize_names: true)
73
+ result[:data_count] = response.headers[:x_count].to_i unless response.headers[:x_count].nil?
74
+ result[:data_pages] = response.headers[:x_pages].to_i unless response.headers[:x_pages].nil?
75
+ Marfa.cache.set(cache_key, result.to_json, params[:cache_time] || 7200)
76
+ end
65
77
  rescue => exception
66
78
  if [:development, :test].include? Marfa.config.environment
67
79
  _log_exception(exception, path, params)
68
80
  end
69
81
  end
82
+ # begin
83
+ # response = RestClient.get("#{Marfa.config.api_server}#{path}", { params: params[:query], headers: {} })
84
+ # result[:data] = JSON.parse(response.body, symbolize_names: true)
85
+ # result[:data_count] = response.headers[:x_count].to_i unless response.headers[:x_count].nil?
86
+ # result[:data_pages] = response.headers[:x_pages].to_i unless response.headers[:x_pages].nil?
87
+ # rescue => exception
88
+ # if [:development, :test].include? Marfa.config.environment
89
+ # _log_exception(exception, path, params)
90
+ # end
91
+ # end
70
92
 
71
93
  result
72
94
  end
data/lib/marfa/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # Version constant
2
2
  module Marfa
3
3
  # The version constant for the current version of Marfa
4
- VERSION = '0.4.7' unless defined?(Marfa::VERSION)
4
+ VERSION = '0.5.0' unless defined?(Marfa::VERSION)
5
5
 
6
6
  # The current Marfa version.
7
7
  # @return [String] The version number
data/marfa.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'marfa'
6
- s.version = '0.4.7'
6
+ s.version = '0.5.0'
7
7
  s.required_ruby_version = '>= 2.3.0'
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.date = Time.now.strftime('%Y-%m-%d')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marfa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Krechetov