rails_json_serializer 2.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: d4ed73e603bb8c02b8999399e76e1d3e359d502db69ede57a800229e1c9a12f9
4
- data.tar.gz: 97cb1d7ac4a6b975d57f23406828783242121f0b3c63f57243e0ed125525733a
3
+ metadata.gz: d3fc5e7e81a63f10f7944eed51908b6b0c64b348398b78119a2cb5613a5624d5
4
+ data.tar.gz: 107b8bb149af177a79c96ed017b42ebe3008d1685811225de3b25209d01a780c
5
5
  SHA512:
6
- metadata.gz: ea0aa2817b2c46c27ff246d0b6e9df2bfbab06ad429d2e99a53f75bb258e81bb3cf9c5f27038992b0ff0037a270815fe9bde69524d3f7ed741bed7ca64f3138a
7
- data.tar.gz: 721fefb41c3ed4892a0899f370eff95e2fdda0d77810137e379fcc1eed3241001c22bda2ebc232d321c99f365479f2757b959c2386febafba965b8d470281663
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
@@ -23,6 +23,20 @@ module ModelSerializer
23
23
  # Inject class methods, will have access to those queries on the class.
24
24
  klass.send(:extend, serializer_klass)
25
25
 
26
+ # Class method to clear the cache of objects without having to instantiate them.
27
+ def self.clear_serializer_cache id_or_ids
28
+ if !id_or_ids.is_a?(Array)
29
+ id_or_ids = [id_or_ids]
30
+ end
31
+ id_or_ids.each do |object_id|
32
+ self::SERIALIZER_QUERY_KEYS_CACHE.each do |query_name|
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
+ Rails.cache.delete(cache_key)
36
+ end
37
+ end
38
+ end
39
+
26
40
  # no need to define it if inheriting class has defined it OR has been manually overridden.
27
41
  # CLASS METHODS
28
42
  klass.send(:define_singleton_method, :serializer) do |opts = {}|
@@ -107,18 +121,31 @@ module ModelSerializer
107
121
  return super(options)
108
122
  end
109
123
  options[:ran_serialization] = true
124
+
110
125
  # Not caching records that don't have IDs.
111
126
  if !Serializer.configuration.disable_model_caching && self.id && options[:cache_key].present? && !(options.key?(:cache_for) && options[:cache_for].nil?)
112
- 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
+
113
129
  if Rails.cache.exist?(cache_key)
114
- Rails.logger.info "Serializer: Cache reading #{cache_key}" if Serializer.configuration.debug
115
- 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
116
136
  else
117
137
  data = super(options)
118
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
+
119
146
  begin
120
- Rails.logger.info "Serializer: Caching #{cache_key} for #{(options[:cache_for] || Serializer.configuration.default_cache_time)} minutes." if Serializer.configuration.debug
121
- 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)
122
149
  rescue Exception => e
123
150
  Rails.logger.error "Serializer: Internal Server Error on #{self.class}#as_json ID: #{self.id} for cache key: #{cache_key}"
124
151
  Rails.logger.error e.class
@@ -129,7 +156,7 @@ module ModelSerializer
129
156
  end
130
157
  else
131
158
  if Serializer.configuration.debug && !Serializer.configuration.disable_model_caching && self.id && options[:cache_key].present? && options.key?(:cache_for) && options[:cache_for].nil?
132
- 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`"
133
160
  end
134
161
  data = super(options)
135
162
  data = self.class.as_json_associations_alias_fix(options, data)
@@ -153,8 +180,8 @@ module ModelSerializer
153
180
  klass.send(:define_method, :clear_serializer_cache) do
154
181
  if self.class.const_defined?("SERIALIZER_QUERY_KEYS_CACHE")
155
182
  self.class::SERIALIZER_QUERY_KEYS_CACHE.each do |query_name|
156
- cache_key = "#{self.class.name}_____#{query_name}___#{self.id}"
157
- 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
158
185
  Rails.cache.delete(cache_key)
159
186
  end
160
187
  return true
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_json_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - benjamin.dana.software.dev@gmail.com
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2020-04-22 00:00:00.000000000 Z
@@ -14,30 +14,44 @@ dependencies:
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.1'
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
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
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: '5.1'
40
+ version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rails
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '5.1'
47
+ version: '5.0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '5.1'
54
+ version: '5.0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -108,8 +122,8 @@ dependencies:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
124
  version: '1.4'
111
- description:
112
- email:
125
+ description:
126
+ email:
113
127
  executables: []
114
128
  extensions: []
115
129
  extra_rdoc_files: []
@@ -121,7 +135,7 @@ homepage: https://github.com/danabr75/rails_json_serializer
121
135
  licenses:
122
136
  - LGPL-3.0-only
123
137
  metadata: {}
124
- post_install_message:
138
+ post_install_message:
125
139
  rdoc_options: []
126
140
  require_paths:
127
141
  - lib
@@ -136,8 +150,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
150
  - !ruby/object:Gem::Version
137
151
  version: '0'
138
152
  requirements: []
139
- rubygems_version: 3.0.8
140
- signing_key:
153
+ rubygems_version: 3.1.6
154
+ signing_key:
141
155
  specification_version: 4
142
156
  summary: An ActiveRecord JSON Serializer with supported caching and eager-loading
143
157
  test_files: []