rails-brotli-cache 0.2.0 → 0.2.1

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: 01d8f09dfdea5ea7f2c19ce63e44b88322b319bfcda33f9efeeb679710e78f87
4
- data.tar.gz: bd6df6fadfd6206ed598dab1ced924d59e5c27da6f1c8a0eaeed434f934219c6
3
+ metadata.gz: 44c290253b45a487daecc85b45026bd7d3f140644095c7f15cfb67dcee1299b8
4
+ data.tar.gz: d37323f26dda367401403714eb094dfd6a0e3274b7d88cb81eb2d5380ce91979
5
5
  SHA512:
6
- metadata.gz: 7919aaad175395d8494f20f1e40b6b5d96e6bd68a3aa60036dd0aa9682b8251e06139fa56748cfe4a5660be3aea5f09f9b7609806336b47b954cefadca227c1d
7
- data.tar.gz: 41ddc8e815662f7f0e9037e5b0beaeacf48cb62eac1c05380b3be44664ed951256cf185d2ecc730f406f7b8a7ee615cad782e68f037907a37b56a1a658766f1d
6
+ metadata.gz: 537fe80385fbc5f8d339b56f789aaeefc45e2589456e2fb522afdcb3656af6254951654a0524b5136ad3cb25fb23c092ea6bc027377c39ed9f52645886a94fc0
7
+ data.tar.gz: a6057e3a65505003ee849e9f0a38a12963a78468c32275ca0e1d70ead490ddba7663d2a1d65b4359ca7fb626b27e15fc8552d77d9d35c353bbdf77f933fdb277
data/README.md CHANGED
@@ -9,8 +9,8 @@ This gem enables support for compressing Ruby on Rails cache entries using the [
9
9
  Brotli cache works as a proxy layer wrapping the underlying cache data store.
10
10
 
11
11
  ```ruby
12
- default_cache = ActiveSupport::Cache::RedisCacheStore.new(redis: $redis)
13
- brotli_cache = RailsBrotliCache::Store.new(default_cache)
12
+ redis_cache = ActiveSupport::Cache::RedisCacheStore.new
13
+ brotli_redis_cache = RailsBrotliCache::Store.new(redis_cache)
14
14
  ```
15
15
 
16
16
  **~25%** better compression of a sample JSON object:
@@ -18,8 +18,8 @@ brotli_cache = RailsBrotliCache::Store.new(default_cache)
18
18
  ```ruby
19
19
  json = File.read("sample.json") # sample 435kb JSON text
20
20
  json.size # => 435662
21
- default_cache.write("json", json)
22
- brotli_cache.write("json", json)
21
+ redis_cache.write("json", json)
22
+ brotli_redis_cache.write("json", json)
23
23
 
24
24
  ## Check the size of cache entry stored in Redis
25
25
  $redis.get("json").size # => 31698
@@ -30,8 +30,8 @@ $redis.get("br-json").size # => 24058
30
30
 
31
31
  ```ruby
32
32
  users = User.limit(100).to_a # 100 ActiveRecord objects
33
- default_cache.write("users", users)
34
- brotli_cache.write("users", users)
33
+ redis_cache.write("users", users)
34
+ brotli_redis_cache.write("users", users)
35
35
  $redis.get("users").size # => 12331
36
36
  $redis.get("br-users").size # => 10299
37
37
  ```
@@ -39,27 +39,44 @@ $redis.get("br-users").size # => 10299
39
39
  **~25%** faster performance for reading/writing a larger JSON file:
40
40
 
41
41
  ```ruby
42
- json = File.read("sample.json") # sample 435kb JSON text
42
+ json = File.read("sample.json") # sample ~1mb JSON text
43
43
 
44
44
  Benchmark.bm do |x|
45
- x.report("default_cache") do
46
- 1000.times do
47
- default_cache.write("test", json)
48
- default_cache.read("test")
45
+ x.report("redis_cache") do
46
+ 100.times do
47
+ redis_cache.write("test", json)
48
+ redis_cache.read("test")
49
49
  end
50
50
  end
51
51
 
52
- x.report("brotli_cache") do
53
- 1000.times do
54
- brotli_cache.write("test", json)
55
- brotli_cache.read("test")
52
+ x.report("brotli_redis_cache") do
53
+ 100.times do
54
+ brotli_redis_cache.write("test", json)
55
+ brotli_redis_cache.read("test")
56
56
  end
57
57
  end
58
+
59
+ # ...
58
60
  end
59
61
 
60
- # user system total real
61
- # default_cache 5.177678 0.216435 5.394113 ( 8.296072)
62
- # brotli_cache 3.513312 0.323601 3.836913 ( 6.114179)
62
+ # memory_cache 2.081221 0.051615 2.132836 ( 2.132877)
63
+ # brotli_memory_cache 1.134411 0.032996 1.167407 ( 1.167418)
64
+ # redis_cache 1.782225 0.049936 1.832161 ( 2.523317)
65
+ # brotli_redis_cache 1.218365 0.051084 1.269449 ( 1.850894)
66
+ # memcached_cache 1.766268 0.045351 1.811619 ( 2.504233)
67
+ # brotli_memcached_cache 1.194646 0.051750 1.246396 ( 1.752982)
68
+ ```
69
+
70
+ Regardless of the underlying data store, Brotli cache offers between 20%-40% performance improvment.
71
+
72
+ You can run the benchmarks yourself by executing:
73
+
74
+ ```ruby
75
+ cp docker-compose.yml.sample docker-compose.yml
76
+ docker compose up -d
77
+ cd benchmarks
78
+ bundle install
79
+ ruby main.rb
63
80
  ```
64
81
 
65
82
  ## Configuration
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails-brotli-cache'
@@ -0,0 +1,69 @@
1
+ require 'rails'
2
+ require 'net/http'
3
+ require 'rails-brotli-cache'
4
+
5
+ memory_cache = ActiveSupport::Cache::MemoryStore.new(compress: true) # memory store does not use compression by default
6
+ brotli_memory_cache = RailsBrotliCache::Store.new(memory_cache)
7
+ redis_cache = ActiveSupport::Cache::RedisCacheStore.new
8
+ brotli_redis_cache = RailsBrotliCache::Store.new(redis_cache)
9
+ memcached_cache = ActiveSupport::Cache::MemCacheStore.new
10
+ brotli_memcached_cache = RailsBrotliCache::Store.new(memcached_cache)
11
+
12
+ json_uri = URI("https://raw.githubusercontent.com/pawurb/rails-brotli-cache/main/spec/fixtures/sample.json")
13
+ json = Net::HTTP.get(json_uri)
14
+
15
+ puts "Uncompressed JSON size: #{json.size}"
16
+ redis_cache.write("gz-json", json)
17
+ gzip_json_size = redis_cache.redis.get("gz-json").size
18
+ puts "Gzip JSON size: #{gzip_json_size}"
19
+ brotli_redis_cache.write("json", json)
20
+ br_json_size = redis_cache.redis.get("br-json").size
21
+ puts "Brotli JSON size: #{br_json_size}"
22
+ puts "~#{((gzip_json_size - br_json_size).to_f / gzip_json_size.to_f * 100).round}% improvment"
23
+ puts ""
24
+
25
+ iterations = 100
26
+
27
+ Benchmark.bm do |x|
28
+ x.report("memory_cache") do
29
+ iterations.times do
30
+ memory_cache.write("test", json)
31
+ memory_cache.read("test")
32
+ end
33
+ end
34
+
35
+ x.report("brotli_memory_cache") do
36
+ iterations.times do
37
+ brotli_memory_cache.write("test", json)
38
+ brotli_memory_cache.read("test")
39
+ end
40
+ end
41
+
42
+ x.report("redis_cache") do
43
+ iterations.times do
44
+ redis_cache.write("test", json)
45
+ redis_cache.read("test")
46
+ end
47
+ end
48
+
49
+ x.report("brotli_redis_cache") do
50
+ iterations.times do
51
+ brotli_redis_cache.write("test", json)
52
+ brotli_redis_cache.read("test")
53
+ end
54
+ end
55
+
56
+ x.report("memcached_cache") do
57
+ iterations.times do
58
+ memcached_cache.write("test", json)
59
+ memcached_cache.read("test")
60
+ end
61
+ end
62
+
63
+ x.report("brotli_memcached_cache") do
64
+ iterations.times do
65
+ brotli_memcached_cache.write("test", json)
66
+ brotli_memcached_cache.read("test")
67
+ end
68
+ end
69
+ end
@@ -75,7 +75,7 @@ module RailsBrotliCache
75
75
  end
76
76
 
77
77
  def disable_prefix!
78
- @@prefix = nil
78
+ @prefix = nil
79
79
  end
80
80
 
81
81
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsBrotliCache
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -73,12 +73,4 @@ describe RailsBrotliCache do
73
73
  expect(cache_store.read("test-key")).to eq nil
74
74
  end
75
75
  end
76
-
77
- describe "disable_prefix!" do
78
- it "saves brotli cache entries without `br-` prefix" do
79
- cache_store.disable_prefix!
80
- cache_store.fetch("test-key") { 123 }
81
- cache_store.instance_variable_set(:@prefix, "br-")
82
- end
83
- end
84
76
  end
@@ -27,4 +27,17 @@ describe RailsBrotliCache do
27
27
  cache_store.write("test-key", json)
28
28
  expect($redis.get("gz-test-key").size > $redis.get("br-test-key").size).to eq true
29
29
  end
30
+
31
+ describe "disable_prefix!" do
32
+ it "saves brotli cache entries without `br-` prefix" do
33
+ cache_store.fetch("test-key") { 123 }
34
+ expect($redis.get("test-key")).to eq nil
35
+ expect($redis.get("br-test-key")).to be_present
36
+ cache_store.disable_prefix!
37
+ cache_store.fetch("test-key-2") { 123 }
38
+ expect($redis.get("br-test-key-2")).to eq nil
39
+ expect($redis.get("test-key-2")).to be_present
40
+ cache_store.instance_variable_set(:@prefix, "br-")
41
+ end
42
+ end
30
43
  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.0
4
+ version: 0.2.1
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-18 00:00:00.000000000 Z
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,6 +108,8 @@ files:
108
108
  - LICENSE.txt
109
109
  - README.md
110
110
  - Rakefile
111
+ - benchmarks/Gemfile
112
+ - benchmarks/main.rb
111
113
  - docker-compose.yml.sample
112
114
  - lib/rails-brotli-cache.rb
113
115
  - lib/rails-brotli-cache/store.rb