rails-brotli-cache 0.4.2 → 0.4.4

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: 733ccaba5b7f8c9e419556a31a8abc1c5a21d3b6421c2b8c3448d8f1faa5f75c
4
- data.tar.gz: 9b1c4366b7cc1c49cd31eb7258c01dfda204f31fcc1a9ce55dd28035b5c83ce1
3
+ metadata.gz: 25d1a71b5e57f6f4c61612ff8c4cfb99d88aba2ed41b8f2436191c5c3e31904b
4
+ data.tar.gz: c9305dc9477b54c0072395d6e55ede962b9c311e907025c84d1ab5dc6fdf648d
5
5
  SHA512:
6
- metadata.gz: 2aadb3e61be2fefb5739c92ccb477605e6550224294ec4f4eabc71d21de068ef4b93f307a02d6e8633ab6001bc98f4c477f961811d30ee88d7a25d3ec5b04157
7
- data.tar.gz: 0cfc2f0747af9d2a088f69a0c8b68d765b01ac9f1448ebafcd73a7dfa4c0dc0d0e60fd2a49b1ba6438f6bef3a5936bb19850db57282e9af2ff56c49f7e0cc4c6
6
+ metadata.gz: b890c5e6b89cd6150fb5e965adecfa2963cce3de9ef4a474c70a66ba7e91f5dc21bba69eed3963e7ec84ddece396cc22be18150a2d0dc54fcf0ea92c4b7be877
7
+ data.tar.gz: 00d8463045555382d2d59bfff056470c6066042860332d30b9cd127166be119a70c28bf39241c144413a032a6fbc87127872945213d56fbb8bf52bf86d36a73c
@@ -46,7 +46,7 @@ module RailsBrotliCache
46
46
  end
47
47
 
48
48
  uncompressed(
49
- @core_store.fetch(cache_key(name), options.merge(compress: false)) do
49
+ @core_store.fetch(expanded_cache_key(name), options.merge(compress: false)) do
50
50
  if block_given?
51
51
  compressed(block.call, options)
52
52
  else
@@ -62,7 +62,7 @@ module RailsBrotliCache
62
62
  payload = compressed(value, options)
63
63
 
64
64
  @core_store.write(
65
- cache_key(name),
65
+ expanded_cache_key(name),
66
66
  payload,
67
67
  options.merge(compress: false)
68
68
  )
@@ -72,7 +72,7 @@ module RailsBrotliCache
72
72
  options = (options || {}).reverse_merge(@init_options)
73
73
 
74
74
  payload = @core_store.read(
75
- cache_key(name),
75
+ expanded_cache_key(name),
76
76
  options
77
77
  )
78
78
 
@@ -83,7 +83,7 @@ module RailsBrotliCache
83
83
  options = (options || {}).reverse_merge(@init_options)
84
84
  new_hash = hash.map do |key, val|
85
85
  [
86
- cache_key(key),
86
+ expanded_cache_key(key),
87
87
  compressed(val, options)
88
88
  ]
89
89
  end
@@ -96,17 +96,17 @@ module RailsBrotliCache
96
96
 
97
97
  def read_multi(*names)
98
98
  options = names.extract_options!
99
- names = names.map { |name| cache_key(name) }
99
+ names = names.map { |name| expanded_cache_key(name) }
100
100
  options = options.reverse_merge(@init_options)
101
101
 
102
- Hash[core_store.read_multi(*names, options).map do |key, val|
102
+ core_store.read_multi(*names, options).map do |key, val|
103
103
  [source_cache_key(key), uncompressed(val, options)]
104
- end]
104
+ end.to_h
105
105
  end
106
106
 
107
107
  def fetch_multi(*names)
108
108
  options = names.extract_options!
109
- names = names.map { |name| cache_key(name) }
109
+ names = names.map { |name| expanded_cache_key(name) }
110
110
  options = options.reverse_merge(@init_options)
111
111
 
112
112
  @core_store.fetch_multi(
@@ -116,24 +116,24 @@ module RailsBrotliCache
116
116
  end
117
117
  end
118
118
 
119
- def exist?(name, options = nil)
120
- @core_store.exist?(cache_key(name), options)
119
+ def exist?(name, options = {})
120
+ @core_store.exist?(expanded_cache_key(name), options)
121
121
  end
122
122
 
123
- def delete(name, options = nil)
124
- @core_store.delete(cache_key(name), options)
123
+ def delete(name, options = {})
124
+ @core_store.delete(expanded_cache_key(name), options)
125
125
  end
126
126
 
127
- def clear(options = nil)
127
+ def clear
128
128
  @core_store.clear
129
129
  end
130
130
 
131
131
  def increment(name, amount = 1, **options)
132
- @core_store.increment(cache_key(name), amount, **options)
132
+ @core_store.increment(expanded_cache_key(name), amount, **options)
133
133
  end
134
134
 
135
135
  def decrement(name, amount = 1, **options)
136
- @core_store.decrement(cache_key(name), amount, **options)
136
+ @core_store.decrement(expanded_cache_key(name), amount, **options)
137
137
  end
138
138
 
139
139
  def self.supports_cache_versioning?
@@ -174,8 +174,8 @@ module RailsBrotliCache
174
174
  Marshal.load(serialized)
175
175
  end
176
176
 
177
- def cache_key(name)
178
- "#{@prefix}#{name}"
177
+ def expanded_cache_key(name)
178
+ "#{@prefix}#{::ActiveSupport::Cache.expand_cache_key(name)}"
179
179
  end
180
180
 
181
181
  def source_cache_key(name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsBrotliCache
4
- VERSION = "0.4.2"
4
+ VERSION = "0.4.4"
5
5
  end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ return unless ENV['RAILS_CACHE_STORE'] == 'redis_cache_store'
6
+
7
+ describe RailsBrotliCache do
8
+ class Post
9
+ include ActiveModel::Model
10
+
11
+ attr_accessor :id
12
+
13
+ def to_param
14
+ "post/#{id}"
15
+ end
16
+ end
17
+
18
+ let(:options) do
19
+ {}
20
+ end
21
+
22
+ let(:redis_store) do
23
+ ActiveSupport::Cache::RedisCacheStore.new({ redis: $redis }.merge(options))
24
+ end
25
+
26
+ let(:brotli_store) do
27
+ RailsBrotliCache::Store.new(
28
+ ActiveSupport::Cache::RedisCacheStore.new(redis: $redis),
29
+ options
30
+ )
31
+ end
32
+
33
+ describe "generated cache keys are identical to standard cache stores" do
34
+ it "works for string keys" do
35
+ redis_store.fetch("string-key") { 123 }
36
+ brotli_store.fetch("string-key") { 123 }
37
+ expect($redis.get("br-string-key")).to be_present
38
+ expect($redis.get("string-key")).to be_present
39
+ end
40
+
41
+ it "ActiveModel object keys" do
42
+ post_1 = Post.new(id: 1)
43
+ redis_store.fetch(post_1) { 123 }
44
+ brotli_store.fetch(post_1) { 123 }
45
+ expect($redis.get("br-post/1")).to be_present
46
+ expect($redis.get("post/1")).to be_present
47
+ end
48
+
49
+ it "ActiveModel objects complex collection keys" do
50
+ post_1 = Post.new(id: 1)
51
+ post_2 = Post.new(id: 2)
52
+ collection = [post_1, post_2]
53
+ redis_store.fetch([:views, "controller/action", collection]) { 123 }
54
+ brotli_store.fetch([:views, "controller/action", collection]) { 123 }
55
+ expect($redis.get("views/controller/action/post/1/post/2")).to be_present
56
+ expect($redis.get("br-views/controller/action/post/1/post/2")).to be_present
57
+ expect(brotli_store.read([:views, "controller/action", collection])).to eq 123
58
+ end
59
+
60
+ context "custom namespace string is not duplicated" do
61
+ let(:options) do
62
+ {
63
+ namespace: "myapp"
64
+ }
65
+ end
66
+
67
+ it "activemodel object keys" do
68
+ post_1 = Post.new(id: 1)
69
+ redis_store.fetch(post_1) { 123 }
70
+ brotli_store.fetch(post_1) { 123 }
71
+ expect($redis.get("myapp:post/1")).to be_present
72
+ expect($redis.get("myapp:br-post/1")).to be_present
73
+ expect(brotli_store.read(post_1)).to eq 123
74
+ end
75
+ end
76
+
77
+ context "custom namespace proc" do
78
+ @@counter = 0
79
+ let(:options) do
80
+ {
81
+ namespace: -> { "myapp" }
82
+ }
83
+ end
84
+
85
+ it "activemodel object keys" do
86
+ post_1 = Post.new(id: 1)
87
+ redis_store.fetch(post_1) { 123 }
88
+ brotli_store.fetch(post_1) { 123 }
89
+ expect($redis.get("myapp:post/1")).to be_present
90
+ expect($redis.get("myapp:br-post/1")).to be_present
91
+ expect(brotli_store.read(post_1)).to eq 123
92
+ end
93
+ end
94
+ end
95
+ end
@@ -28,6 +28,16 @@ describe RailsBrotliCache do
28
28
  end
29
29
  end
30
30
 
31
+ class Post
32
+ include ActiveModel::Model
33
+
34
+ attr_accessor :id
35
+
36
+ def to_param
37
+ "post/#{id}"
38
+ end
39
+ end
40
+
31
41
  describe "#fetch" do
32
42
  it "stores value in the configured Rails.cache with a prefix" do
33
43
  cache_store.fetch("test-key") { 123 }
@@ -45,6 +55,15 @@ describe RailsBrotliCache do
45
55
  expect(cache_store.read("test-key")).to eq 1
46
56
  end
47
57
 
58
+ it "stores and reads fragment caches with complex objects as cache keys" do
59
+ cached_fragment = "<div>Cached fragment</div>"
60
+
61
+ collection = [Post.new(id: 1), Post.new(id: 2)]
62
+
63
+ cache_store.fetch([:views, "controller/action", collection]) { cached_fragment }
64
+ expect(cache_store.read([:views, "controller/action", collection])).to eq(cached_fragment)
65
+ end
66
+
48
67
  context "{ force: true }" do
49
68
  it "raises an error if block is not provided" do
50
69
  expect {
@@ -127,6 +146,15 @@ describe RailsBrotliCache do
127
146
  expect(cache_store.read("test-key")).to eq big_enough_to_compress_value
128
147
  end
129
148
 
149
+ it "writes and reads fragment caches with complex objects as cache keys" do
150
+ cached_fragment = "<div>Cached fragment</div>"
151
+
152
+ collection = [Post.new(id: 1), Post.new(id: 2)]
153
+
154
+ cache_store.write([:views, "controller/action", [collection, nil]], cached_fragment)
155
+ expect(cache_store.read([:views, "controller/action", [collection, nil]])).to eq(cached_fragment)
156
+ end
157
+
130
158
  describe ":compressor_class option" do
131
159
  context "as an init config" do
132
160
  let(:options) do
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.4.2
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-14 00:00:00.000000000 Z
11
+ date: 2023-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -158,6 +158,7 @@ files:
158
158
  - spec/dummy/public/robots.txt
159
159
  - spec/dummy/vendor/.keep
160
160
  - spec/fixtures/sample.json
161
+ - spec/rails-brotli-cache/cache_keys_spec.rb
161
162
  - spec/rails-brotli-cache/compatibility_spec.rb
162
163
  - spec/rails-brotli-cache/rails_store_spec.rb
163
164
  - spec/rails-brotli-cache/redis_spec.rb
@@ -231,6 +232,7 @@ test_files:
231
232
  - spec/dummy/public/robots.txt
232
233
  - spec/dummy/vendor/.keep
233
234
  - spec/fixtures/sample.json
235
+ - spec/rails-brotli-cache/cache_keys_spec.rb
234
236
  - spec/rails-brotli-cache/compatibility_spec.rb
235
237
  - spec/rails-brotli-cache/rails_store_spec.rb
236
238
  - spec/rails-brotli-cache/redis_spec.rb