rails-brotli-cache 0.3.6 → 0.3.8

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: 2d61874a96a2402ac69abd17762f98531de0a5b2c2749447f3c34d03dc76b57b
4
- data.tar.gz: dff930d1caf6e592d207db48ffb175ebcd06bc59d228a9b5a78e3185672ee72d
3
+ metadata.gz: 3bb8214df1fb2d155f5ae121938b65e447206dba8480066ce6fe0ae3224a18f5
4
+ data.tar.gz: 9831643f175b1e5b7d8904ca696302640bd7fc5f3216f93575795ee4192d1fd1
5
5
  SHA512:
6
- metadata.gz: f3d3cda105423ff917c8b7bd1785cbad9c4577fe773d9573995f1341a6fb3093538e67febe45e6c4449c80398c5edfc95770c7b968fae78524f55bdefa92bd92
7
- data.tar.gz: b705720c468a0c85e2b7cf3ff72a2c320ab0beefa3d0889c862b716d1d8d26146e5f2afe9f08bb2101e0ddc07e313cf06a85095f6b778ab8d11ef3f4977a4783
6
+ metadata.gz: 0c8297ac281da41e9923721bc24984aa5f795eede3748ee93aa4a47c2d614dfbfe5641255217b010d126852723c69858f27b58f985125fd1b024472641ac46ff
7
+ data.tar.gz: 7ad44d4c2c4f1a9411d13fa44d11cbea56b262328e1a62b72f6b14ad8d397f6a53cc09542d5b918d80b504bcd4df6f27c08cec8ae4467a26ab494f1644ab1159
@@ -49,20 +49,8 @@ module RailsBrotliCache
49
49
  )
50
50
  end
51
51
 
52
- serialized = Marshal.dump(value)
53
52
  options = (options || {}).reverse_merge(compress: true)
54
-
55
- payload = if serialized.bytesize >= COMPRESS_THRESHOLD && !options.fetch(:compress) == false
56
- compressor = compressor_class(options, default: @compressor_class)
57
- compressed_payload = compressor.deflate(serialized)
58
- if compressed_payload.bytesize < serialized.bytesize
59
- MARK_BR_COMPRESSED + compressed_payload
60
- else
61
- serialized
62
- end
63
- else
64
- serialized
65
- end
53
+ payload = compressed(value, options)
66
54
 
67
55
  @core_store.write(
68
56
  cache_key(name),
@@ -77,21 +65,39 @@ module RailsBrotliCache
77
65
  options
78
66
  )
79
67
 
80
- return nil unless payload.present?
81
-
82
- return payload if payload.is_a?(Integer)
68
+ uncompressed(payload)
69
+ end
83
70
 
84
- serialized = if payload.start_with?(MARK_BR_COMPRESSED)
85
- compressor = compressor_class(options, default: @compressor_class)
86
- compressor.inflate(payload.byteslice(1..-1))
87
- else
88
- payload
71
+ def write_multi(hash, options = nil)
72
+ new_hash = hash.map do |key, val|
73
+ [
74
+ cache_key(key),
75
+ compressed(val, options)
76
+ ]
89
77
  end
90
78
 
91
- Marshal.load(serialized)
79
+ @core_store.write_multi(new_hash, options)
92
80
  end
93
81
 
94
- def exist?(name, options = nil)
82
+ def read_multi(*names)
83
+ options = names.extract_options!
84
+ names = names.map { |name| cache_key(name) }
85
+
86
+ Hash[core_store.read_multi(*names, options).map do |key, val|
87
+ [source_cache_key(key), uncompressed(val)]
88
+ end]
89
+ end
90
+
91
+ def fetch_multi(*names)
92
+ options = names.extract_options!
93
+ names = names.map { |name| cache_key(name) }
94
+
95
+ @core_store.fetch_multi(*names, options) do |name|
96
+ compressed(yield(name), options)
97
+ end
98
+ end
99
+
100
+ def exist?(name, options = nil)
95
101
  @core_store.exist?(cache_key(name), options)
96
102
  end
97
103
 
@@ -119,6 +125,38 @@ module RailsBrotliCache
119
125
 
120
126
  private
121
127
 
128
+ def compressed(value, options)
129
+ options ||= {}
130
+ serialized = Marshal.dump(value)
131
+
132
+ if serialized.bytesize >= COMPRESS_THRESHOLD && !options.fetch(:compress) == false
133
+ compressor = compressor_class(options, default: @compressor_class)
134
+ compressed_payload = compressor.deflate(serialized)
135
+ if compressed_payload.bytesize < serialized.bytesize
136
+ MARK_BR_COMPRESSED + compressed_payload
137
+ else
138
+ serialized
139
+ end
140
+ else
141
+ serialized
142
+ end
143
+ end
144
+
145
+ def uncompressed(payload)
146
+ return nil unless payload.present?
147
+
148
+ return payload if payload.is_a?(Integer)
149
+
150
+ serialized = if payload.start_with?(MARK_BR_COMPRESSED)
151
+ compressor = compressor_class(options, default: @compressor_class)
152
+ compressor.inflate(payload.byteslice(1..-1))
153
+ else
154
+ payload
155
+ end
156
+
157
+ Marshal.load(serialized)
158
+ end
159
+
122
160
  def compressor_class(options, default:)
123
161
  options = options || {}
124
162
  if (klass = options[:compressor_class])
@@ -132,6 +170,10 @@ module RailsBrotliCache
132
170
  "#{@prefix}#{name}"
133
171
  end
134
172
 
173
+ def source_cache_key(name)
174
+ name.remove(@prefix)
175
+ end
176
+
135
177
  class BrotliCompressor
136
178
  def self.deflate(payload)
137
179
  ::Brotli.deflate(payload, quality: BR_COMPRESS_QUALITY)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsBrotliCache
4
- VERSION = "0.3.6"
4
+ VERSION = "0.3.8"
5
5
  end
@@ -60,6 +60,41 @@ describe RailsBrotliCache do
60
60
  expect(brotli_store.fetch("val_key", force: true) { val }).to eq(standard_cache.fetch("val_key", force: true) { val })
61
61
  expect(brotli_store.fetch("val_key")).to eq(standard_cache.fetch("val_key"))
62
62
  end
63
+
64
+ it "for #write_multi and #read_multi" do
65
+ values = {
66
+ "key_1" => "val_1",
67
+ "key_2" => "val_2"
68
+ }
69
+
70
+ brotli_store.write_multi(values)
71
+ standard_cache.write_multi(values)
72
+
73
+ expect(brotli_store.read("key_1")).to eq standard_cache.read("key_1")
74
+ expect(brotli_store.read("key_2")).to eq standard_cache.read("key_2")
75
+
76
+ expect(brotli_store.read_multi("key_1", "key_2")).to eq(standard_cache.read_multi("key_1", "key_2"))
77
+ end
78
+
79
+ it "for #fetch_multi" do
80
+ values = {
81
+ "key_1" => "val_1",
82
+ "key_2" => "val_2"
83
+ }
84
+
85
+ brotli_store.fetch_multi("key_1", "key_2") do |key|
86
+ "val_#{key.split('_').last}"
87
+ end
88
+
89
+ standard_cache.fetch_multi("key_1", "key_2") do |key|
90
+ "val_#{key.split('_').last}"
91
+ end
92
+
93
+ expect(brotli_store.read("key_1")).to eq standard_cache.read("key_1")
94
+ expect(brotli_store.read("key_2")).to eq standard_cache.read("key_2")
95
+
96
+ expect(brotli_store.read_multi("key_1", "key_2")).to eq(standard_cache.read_multi("key_1", "key_2"))
97
+ end
63
98
  end
64
99
  end
65
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-brotli-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-26 00:00:00.000000000 Z
11
+ date: 2023-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails