rails_json_serializer 3.0.2 → 3.1.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
  SHA256:
3
- metadata.gz: 661c8d4dea0b016447621acbca1c536e3274e4e70a272e3a67610f380f987833
4
- data.tar.gz: 642499c67e8e2b7814bc1e550f95f841edfc7895178b3f5483626b2978ea3ba2
3
+ metadata.gz: d3fc5e7e81a63f10f7944eed51908b6b0c64b348398b78119a2cb5613a5624d5
4
+ data.tar.gz: 107b8bb149af177a79c96ed017b42ebe3008d1685811225de3b25209d01a780c
5
5
  SHA512:
6
- metadata.gz: 0e5964304d7d90cef99365f2df5279ef2edaa3983879f07bcac983ae69ebc94baab7d987be66ef4b578060e147f2c9c294f2144f208c3376d7531d714e4c6b55
7
- data.tar.gz: ca7f0b5465661b46b985c12797c559e7d795ebebce684331835a7b5a2def49b5cbe88fe72c649f42eb5258cc353698f28007021ee703fb7be9491668ac6fca81
6
+ metadata.gz: '083495f8ec6a05a677273d64d86ac31920d6f37b4b55cb81995812d7f24520630562cfce1905d87cb91bc8c31284a60430aef164dc9826b19f435eff24b1ac2b'
7
+ data.tar.gz: 1feb1b497f6e4d5ab03f9ff7e9de02f672990b5555606fe896c58351656f4c3937b75e8334404c87b309a5cd300ec213bb0e556c7f3a686d0601e9137f835495
@@ -1,12 +1,26 @@
1
+ require "zlib"
2
+
1
3
  module Serializer
2
4
  class Configuration
3
- attr_accessor :enable_includes, :default_cache_time, :disable_model_caching, :debug
5
+ attr_accessor :enable_includes, :default_cache_time, :disable_model_caching, :debug, :cache_key
6
+ attr_accessor :compress, :compressor, :decompressor
4
7
 
5
8
  def initialize
6
9
  @enable_includes = true
7
10
  @default_cache_time = 360
8
11
  @disable_model_caching = false
9
12
  @debug = false
13
+ @compress = false
14
+ @cache_key = Proc.new { |class_name, query_name, object_id| "#{class_name}_____#{query_name}___#{object_id}" }
15
+ @compressor = Proc.new { |incoming_data| Base64.encode64(Zlib::Deflate.deflate(incoming_data.to_json)) }
16
+
17
+ # have to use 'temp_val', or else there's an issue with the libraries. Won't work as a 1-liner
18
+ @decompressor = Proc.new do |outgoing_data|
19
+ temp_val1 = Base64.decode64(outgoing_data)
20
+ temp_val2 = Zlib::Inflate.inflate(temp_val1)
21
+ JSON.parse(temp_val2)
22
+ end
23
+
10
24
  end
11
25
  end
12
26
  end
@@ -30,8 +30,8 @@ module ModelSerializer
30
30
  end
31
31
  id_or_ids.each do |object_id|
32
32
  self::SERIALIZER_QUERY_KEYS_CACHE.each do |query_name|
33
- cache_key = "#{self.name}_____#{query_name}___#{object_id}"
34
- puts "(class) CLEARING SERIALIZER CACHE: #{cache_key}" if Rails.env.development?
33
+ cache_key = Serializer.configuration.cache_key.call(self.name, query_name, object_id)
34
+ Rails.logger.debug "(class) CLEARING SERIALIZER CACHE: #{cache_key}" if Serializer.configuration.debug
35
35
  Rails.cache.delete(cache_key)
36
36
  end
37
37
  end
@@ -121,18 +121,31 @@ module ModelSerializer
121
121
  return super(options)
122
122
  end
123
123
  options[:ran_serialization] = true
124
+
124
125
  # Not caching records that don't have IDs.
125
126
  if !Serializer.configuration.disable_model_caching && self.id && options[:cache_key].present? && !(options.key?(:cache_for) && options[:cache_for].nil?)
126
- cache_key = "#{self.class.name}_____#{options[:cache_key]}___#{self.id}"
127
+ cache_key = Serializer.configuration.cache_key.call(self.class.name, options[:cache_key], self.id)
128
+
127
129
  if Rails.cache.exist?(cache_key)
128
- Rails.logger.info "Serializer: Cache reading #{cache_key}" if Serializer.configuration.debug
129
- return Rails.cache.read(cache_key)
130
+ Rails.logger.debug "Serializer: Cache reading #{cache_key}" if Serializer.configuration.debug
131
+ outgoing_data = Rails.cache.read(cache_key)
132
+ if (options.key?(:compress) && options[:compress] == true) || (!options.key?(:compress) && Serializer.configuration.compress)
133
+ outgoing_data = Serializer.configuration.decompressor.call(outgoing_data)
134
+ end
135
+ return outgoing_data
130
136
  else
131
137
  data = super(options)
132
138
  data = self.class.as_json_associations_alias_fix(options, data)
139
+
140
+ # compress data
141
+ cachable_data = data
142
+ if (options.key?(:compress) && options[:compress] == true) || (!options.key?(:compress) && Serializer.configuration.compress)
143
+ cachable_data = Serializer.configuration.compressor.call(data)
144
+ end
145
+
133
146
  begin
134
- Rails.logger.info "Serializer: Caching #{cache_key} for #{(options[:cache_for] || Serializer.configuration.default_cache_time)} minutes." if Serializer.configuration.debug
135
- Rails.cache.write(cache_key, data, expires_in: (options[:cache_for] || Serializer.configuration.default_cache_time).minute)
147
+ Rails.logger.debug "Serializer: Caching #{cache_key} for #{(options[:cache_for] || Serializer.configuration.default_cache_time)} minutes." if Serializer.configuration.debug
148
+ Rails.cache.write(cache_key, cachable_data, expires_in: (options[:cache_for] || Serializer.configuration.default_cache_time).minute)
136
149
  rescue Exception => e
137
150
  Rails.logger.error "Serializer: Internal Server Error on #{self.class}#as_json ID: #{self.id} for cache key: #{cache_key}"
138
151
  Rails.logger.error e.class
@@ -143,7 +156,7 @@ module ModelSerializer
143
156
  end
144
157
  else
145
158
  if Serializer.configuration.debug && !Serializer.configuration.disable_model_caching && self.id && options[:cache_key].present? && options.key?(:cache_for) && options[:cache_for].nil?
146
- Rails.logger.info "Serializer: Caching #{cache_key} NOT caching due to `cache_for: nil`"
159
+ Rails.logger.debug "Serializer: Caching #{cache_key} NOT caching due to `cache_for: nil`"
147
160
  end
148
161
  data = super(options)
149
162
  data = self.class.as_json_associations_alias_fix(options, data)
@@ -167,8 +180,8 @@ module ModelSerializer
167
180
  klass.send(:define_method, :clear_serializer_cache) do
168
181
  if self.class.const_defined?("SERIALIZER_QUERY_KEYS_CACHE")
169
182
  self.class::SERIALIZER_QUERY_KEYS_CACHE.each do |query_name|
170
- cache_key = "#{self.class.name}_____#{query_name}___#{self.id}"
171
- Rails.logger.info "Serializer: CLEARING CACHE KEY: #{cache_key}" if Serializer.configuration.debug
183
+ cache_key = Serializer.configuration.cache_key.call(self.class.name, query_name, self.id)
184
+ Rails.logger.debug "Serializer: CLEARING CACHE KEY: #{cache_key}" if Serializer.configuration.debug
172
185
  Rails.cache.delete(cache_key)
173
186
  end
174
187
  return true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_json_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - benjamin.dana.software.dev@gmail.com
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: zlib
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rails
29
43
  requirement: !ruby/object:Gem::Requirement