rails-brotli-cache 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -8
- data/lib/rails-brotli-cache/store.rb +13 -6
- data/lib/rails-brotli-cache/version.rb +1 -1
- data/spec/rails-brotli-cache/redis_spec.rb +25 -11
- data/spec/rails-brotli-cache/store_spec.rb +6 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f5eae03f927b9f70606cc7b4134fc7173926951da4cf05172bdd9cecb149c61
|
4
|
+
data.tar.gz: c89a69ec5b5d5ddbb8efb432d253784ec30be073ca0a4d142edf347cbc4b75cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b8b3daea70b8c8f5cd8f445ee08155eb6356fbab042db3fd505dc26e666fe8a1011080391940d1d77e08d912ddef46d9a7908377bf6135c7784c433a37ce495
|
7
|
+
data.tar.gz: 5c37c70c355770b708b7703333a4f941d5b28e51d8a6e140eb256ce5a145fa8da21a86cd72af9498f274c56c97d5b3daa9718e8a58aefe4e206d31f58a8d1471
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
# Rails Brotli Cache [![Gem Version](https://
|
1
|
+
# Rails Brotli Cache [![Gem Version](https://img.shields.io/gem/v/rails-brotli-cache)](https://badge.fury.io/rb/rails-brotli-cache) [![CircleCI](https://circleci.com/gh/pawurb/rails-brotli-cache.svg?style=svg)](https://circleci.com/gh/pawurb/rails-brotli-cache)
|
2
2
|
|
3
|
-
This gem enables support for compressing Ruby on Rails cache entries using the [Brotli compression algorithm](https://github.com/google/brotli). `RailsBrotliCache` offers better compression and faster performance compared to the default `Rails.cache` regardless of the underlying data store.
|
4
|
-
|
5
|
-
**The gem is currently in an early stage of development. Ideas on how to improve it and PRs are welcome.**
|
3
|
+
This gem enables support for compressing Ruby on Rails cache entries using the [Brotli compression algorithm](https://github.com/google/brotli). `RailsBrotliCache::Store` offers better compression and faster performance compared to the default `Rails.cache` regardless of the underlying data store.
|
6
4
|
|
7
5
|
## Benchmarks
|
8
6
|
|
@@ -101,14 +99,17 @@ config.cache_store = RailsBrotliCache::Store.new(
|
|
101
99
|
)
|
102
100
|
```
|
103
101
|
|
104
|
-
Gem appends `br-` to the cache key names to prevent conflicts with previously saved cache entries. You can disable this behaviour by
|
105
|
-
|
106
|
-
`app/config/initializers/rails-brotli-cache.rb`
|
102
|
+
Gem appends `br-` to the cache key names to prevent conflicts with previously saved cache entries. You can disable this behaviour by passing `{ prefix: nil }` during initialization:
|
107
103
|
|
108
104
|
```ruby
|
109
|
-
|
105
|
+
config.cache_store = RailsBrotliCache::Store.new(
|
106
|
+
ActiveSupport::Cache::MemoryStore.new,
|
107
|
+
{ prefix: nil }
|
108
|
+
)
|
110
109
|
```
|
111
110
|
|
111
|
+
Addition of the prefix means that you can safely add the Brotli the cache config and avoid compression algorithm conflicts between old and new entries. After configuring the Brotli cache you should run `Rails.cache.clear` to remove the outdated (gzipped) entries.
|
112
|
+
|
112
113
|
## Testing
|
113
114
|
|
114
115
|
```bash
|
@@ -9,9 +9,15 @@ module RailsBrotliCache
|
|
9
9
|
COMPRESS_QUALITY = ENV.fetch("BR_CACHE_COMPRESS_QUALITY", 5).to_i
|
10
10
|
MARK_BR_COMPRESSED = "\x02".b
|
11
11
|
|
12
|
+
attr_reader :core_store
|
13
|
+
|
12
14
|
def initialize(core_store, options = {})
|
13
15
|
@core_store = core_store
|
14
|
-
@prefix =
|
16
|
+
@prefix = if options.key?(:prefix)
|
17
|
+
options.fetch(:prefix)
|
18
|
+
else
|
19
|
+
"br-"
|
20
|
+
end
|
15
21
|
end
|
16
22
|
|
17
23
|
def fetch(name, options = nil, &block)
|
@@ -37,7 +43,12 @@ module RailsBrotliCache
|
|
37
43
|
serialized = Marshal.dump(value)
|
38
44
|
|
39
45
|
payload = if serialized.bytesize >= COMPRESS_THRESHOLD
|
40
|
-
|
46
|
+
compressed_payload = ::Brotli.deflate(serialized, quality: COMPRESS_QUALITY)
|
47
|
+
if compressed_payload.bytesize < serialized.bytesize
|
48
|
+
MARK_BR_COMPRESSED + compressed_payload
|
49
|
+
else
|
50
|
+
serialized
|
51
|
+
end
|
41
52
|
else
|
42
53
|
serialized
|
43
54
|
end
|
@@ -74,10 +85,6 @@ module RailsBrotliCache
|
|
74
85
|
@core_store.clear
|
75
86
|
end
|
76
87
|
|
77
|
-
def disable_prefix!
|
78
|
-
@prefix = nil
|
79
|
-
end
|
80
|
-
|
81
88
|
private
|
82
89
|
|
83
90
|
def cache_key(name)
|
@@ -5,9 +5,14 @@ require 'spec_helper'
|
|
5
5
|
return unless ENV['RAILS_CACHE_STORE'] == 'redis_cache_store'
|
6
6
|
|
7
7
|
describe RailsBrotliCache do
|
8
|
+
let(:options) do
|
9
|
+
{}
|
10
|
+
end
|
11
|
+
|
8
12
|
subject(:cache_store) do
|
9
13
|
RailsBrotliCache::Store.new(
|
10
|
-
ActiveSupport::Cache::RedisCacheStore.new(redis: $redis)
|
14
|
+
ActiveSupport::Cache::RedisCacheStore.new(redis: $redis),
|
15
|
+
options
|
11
16
|
)
|
12
17
|
end
|
13
18
|
|
@@ -28,16 +33,25 @@ describe RailsBrotliCache do
|
|
28
33
|
expect($redis.get("gz-test-key").size > $redis.get("br-test-key").size).to eq true
|
29
34
|
end
|
30
35
|
|
31
|
-
describe "disable_prefix
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
describe "disable_prefix" do
|
37
|
+
context "default prefix" do
|
38
|
+
it "appends 'br-' prefix" do
|
39
|
+
cache_store.fetch("test-key") { 123 }
|
40
|
+
expect($redis.get("test-key")).to eq nil
|
41
|
+
expect($redis.get("br-test-key")).to be_present
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "no prefix" do
|
46
|
+
let(:options) do
|
47
|
+
{ prefix: nil }
|
48
|
+
end
|
49
|
+
|
50
|
+
it "saves brotli cache entries without `br-` prefix" do
|
51
|
+
cache_store.fetch("test-key") { 123 }
|
52
|
+
expect($redis.get("br-test-key")).to eq nil
|
53
|
+
expect($redis.get("test-key")).to be_present
|
54
|
+
end
|
41
55
|
end
|
42
56
|
end
|
43
57
|
end
|
@@ -40,6 +40,12 @@ describe RailsBrotliCache do
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
describe "#core_store" do
|
44
|
+
it "exposes the underlying data store" do
|
45
|
+
expect(cache_store.core_store.class).to eq ActiveSupport::Cache::MemoryStore
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
43
49
|
describe "#read and #write" do
|
44
50
|
it "reads values stored in Rails cache with a prefix" do
|
45
51
|
expect(cache_store.read("test-key")).to eq nil
|
@@ -67,12 +73,4 @@ describe RailsBrotliCache do
|
|
67
73
|
expect(cache_store.read("test-key")).to eq nil
|
68
74
|
end
|
69
75
|
end
|
70
|
-
|
71
|
-
describe "disable_prefix!" do
|
72
|
-
it "saves brotli cache entries without `br-` prefix" do
|
73
|
-
cache_store.disable_prefix!
|
74
|
-
cache_store.fetch("test-key") { 123 }
|
75
|
-
cache_store.instance_variable_set(:@prefix, "br-")
|
76
|
-
end
|
77
|
-
end
|
78
76
|
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.2.
|
4
|
+
version: 0.2.3
|
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-
|
11
|
+
date: 2023-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|