rails-brotli-cache 0.3.7 → 0.3.9
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 +25 -30
- 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: 4d2d42ab4cc77a7965e7d04b70b07a672b657d9b4b54be4814f8716595254d8e
|
4
|
+
data.tar.gz: f81c51e38be663e108538b138fecc80ed726fa701f00e98b658ec028a49528ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd18abde6498cd2fd982deb14eac6150c6cddbad7ec418a24fe5bf8dca78458354b5b5419f572311652369ff0f1c20e9d398c7b340195e48028ef6f3ce39a517
|
7
|
+
data.tar.gz: a5b278df40d8afd4f48e0bf3f1e2c0487b2b3e66e75ecb2c6c937aac483426ea3309081cce6af946e94a88dd984160b5b91ee318129d8b1c92b3a3e41a33d29f
|
@@ -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.delete_prefix(@prefix.to_s)
|
175
|
+
end
|
176
|
+
|
157
177
|
class BrotliCompressor
|
158
178
|
def self.deflate(payload)
|
159
179
|
::Brotli.deflate(payload, quality: BR_COMPRESS_QUALITY)
|
@@ -62,43 +62,38 @@ describe RailsBrotliCache do
|
|
62
62
|
end
|
63
63
|
|
64
64
|
it "for #write_multi and #read_multi" do
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
expect(brotli_store.read_multi("key_1", "key_2")).to eq(standard_cache.read_multi("key_1", "key_2"))
|
79
|
-
end
|
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"))
|
80
77
|
end
|
81
78
|
|
82
79
|
it "for #fetch_multi" do
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
}
|
80
|
+
values = {
|
81
|
+
"key_1" => "val_1",
|
82
|
+
"key_2" => "val_2"
|
83
|
+
}
|
88
84
|
|
89
|
-
|
90
|
-
|
91
|
-
|
85
|
+
brotli_store.fetch_multi("key_1", "key_2") do |key|
|
86
|
+
"val_#{key.split('_').last}"
|
87
|
+
end
|
92
88
|
|
93
|
-
|
94
|
-
|
95
|
-
|
89
|
+
standard_cache.fetch_multi("key_1", "key_2") do |key|
|
90
|
+
"val_#{key.split('_').last}"
|
91
|
+
end
|
96
92
|
|
97
|
-
|
98
|
-
|
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
|
-
|
101
|
-
end
|
96
|
+
expect(brotli_store.read_multi("key_1", "key_2")).to eq(standard_cache.read_multi("key_1", "key_2"))
|
102
97
|
end
|
103
98
|
end
|
104
99
|
end
|