rails-brotli-cache 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rails-brotli-cache/store.rb +51 -31
- data/lib/rails-brotli-cache/version.rb +1 -1
- data/spec/rails-brotli-cache/compatibility_spec.rb +5 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bb8214df1fb2d155f5ae121938b65e447206dba8480066ce6fe0ae3224a18f5
|
4
|
+
data.tar.gz: 9831643f175b1e5b7d8904ca696302640bd7fc5f3216f93575795ee4192d1fd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,39 +65,35 @@ module RailsBrotliCache
|
|
77
65
|
options
|
78
66
|
)
|
79
67
|
|
80
|
-
|
81
|
-
|
82
|
-
return payload if payload.is_a?(Integer)
|
83
|
-
|
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
|
89
|
-
end
|
90
|
-
|
91
|
-
Marshal.load(serialized)
|
68
|
+
uncompressed(payload)
|
92
69
|
end
|
93
70
|
|
94
71
|
def write_multi(hash, options = nil)
|
95
|
-
hash.
|
96
|
-
|
72
|
+
new_hash = hash.map do |key, val|
|
73
|
+
[
|
74
|
+
cache_key(key),
|
75
|
+
compressed(val, options)
|
76
|
+
]
|
97
77
|
end
|
78
|
+
|
79
|
+
@core_store.write_multi(new_hash, options)
|
98
80
|
end
|
99
81
|
|
100
82
|
def read_multi(*names)
|
101
83
|
options = names.extract_options!
|
84
|
+
names = names.map { |name| cache_key(name) }
|
102
85
|
|
103
|
-
Hash[names.map do |
|
104
|
-
[
|
86
|
+
Hash[core_store.read_multi(*names, options).map do |key, val|
|
87
|
+
[source_cache_key(key), uncompressed(val)]
|
105
88
|
end]
|
106
89
|
end
|
107
90
|
|
108
91
|
def fetch_multi(*names)
|
109
92
|
options = names.extract_options!
|
93
|
+
names = names.map { |name| cache_key(name) }
|
110
94
|
|
111
|
-
names
|
112
|
-
|
95
|
+
@core_store.fetch_multi(*names, options) do |name|
|
96
|
+
compressed(yield(name), options)
|
113
97
|
end
|
114
98
|
end
|
115
99
|
|
@@ -141,6 +125,38 @@ module RailsBrotliCache
|
|
141
125
|
|
142
126
|
private
|
143
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
|
+
|
144
160
|
def compressor_class(options, default:)
|
145
161
|
options = options || {}
|
146
162
|
if (klass = options[:compressor_class])
|
@@ -154,6 +170,10 @@ module RailsBrotliCache
|
|
154
170
|
"#{@prefix}#{name}"
|
155
171
|
end
|
156
172
|
|
173
|
+
def source_cache_key(name)
|
174
|
+
name.remove(@prefix)
|
175
|
+
end
|
176
|
+
|
157
177
|
class BrotliCompressor
|
158
178
|
def self.deflate(payload)
|
159
179
|
::Brotli.deflate(payload, quality: BR_COMPRESS_QUALITY)
|
@@ -62,7 +62,6 @@ describe RailsBrotliCache do
|
|
62
62
|
end
|
63
63
|
|
64
64
|
it "for #write_multi and #read_multi" do
|
65
|
-
if standard_cache.class != ActiveSupport::Cache::NullStore ## NullStore does not support read/write multi
|
66
65
|
values = {
|
67
66
|
"key_1" => "val_1",
|
68
67
|
"key_2" => "val_2"
|
@@ -70,17 +69,14 @@ describe RailsBrotliCache do
|
|
70
69
|
|
71
70
|
brotli_store.write_multi(values)
|
72
71
|
standard_cache.write_multi(values)
|
73
|
-
|
74
|
-
expect(brotli_store.read("
|
75
|
-
expect(
|
76
|
-
expect(standard_cache.read("key_2")).to eq "val_2"
|
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")
|
77
75
|
|
78
76
|
expect(brotli_store.read_multi("key_1", "key_2")).to eq(standard_cache.read_multi("key_1", "key_2"))
|
79
|
-
end
|
80
77
|
end
|
81
78
|
|
82
79
|
it "for #fetch_multi" do
|
83
|
-
if standard_cache.class != ActiveSupport::Cache::NullStore ## NullStore does not support fetch multi
|
84
80
|
values = {
|
85
81
|
"key_1" => "val_1",
|
86
82
|
"key_2" => "val_2"
|
@@ -94,11 +90,10 @@ describe RailsBrotliCache do
|
|
94
90
|
"val_#{key.split('_').last}"
|
95
91
|
end
|
96
92
|
|
97
|
-
expect(brotli_store.read("key_1")).to eq "
|
98
|
-
expect(brotli_store.read("key_2")).to eq "
|
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")
|
99
95
|
|
100
96
|
expect(brotli_store.read_multi("key_1", "key_2")).to eq(standard_cache.read_multi("key_1", "key_2"))
|
101
|
-
end
|
102
97
|
end
|
103
98
|
end
|
104
99
|
end
|