rails-brotli-cache 0.4.2 → 0.4.3

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: 52fe3544e2a3d65acf1bba0cd17939e62d628f1dcd6482ec7110c730494527a8
4
+ data.tar.gz: 9e3d9691e99051ce29ed22ccb3a7414e6a00c8037fbc877ef8892a4f4e2a2705
5
5
  SHA512:
6
- metadata.gz: 2aadb3e61be2fefb5739c92ccb477605e6550224294ec4f4eabc71d21de068ef4b93f307a02d6e8633ab6001bc98f4c477f961811d30ee88d7a25d3ec5b04157
7
- data.tar.gz: 0cfc2f0747af9d2a088f69a0c8b68d765b01ac9f1448ebafcd73a7dfa4c0dc0d0e60fd2a49b1ba6438f6bef3a5936bb19850db57282e9af2ff56c49f7e0cc4c6
6
+ metadata.gz: d838530dedd6aea82be35754f96e9269171fe2c62f1b8654dcd0a439836d24c71b1e4d60d09931f6ed9a32d57ca6f4fe8a3e4623d04647ca8e3d46a6c020ef75
7
+ data.tar.gz: '058b95a3d43191462cc98b2bc0086e37b88175aff01fbf8eb6b134eb757622a5fd999acadd39594c3b6818df36103049f57917031a26111726f6b4e722109441'
@@ -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[:namespace]), 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, options[:namespace]),
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, options[:namespace]),
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, options[:namespace]),
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, options[:namespace]) }
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, options[:namespace]) }
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[:namespace]), 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[:namespace]), 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, options[:namespace]), 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, options[:namespace]), 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, namespace = nil)
178
+ "#{@prefix}#{::ActiveSupport::Cache.expand_cache_key(name, namespace)}"
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.3"
5
5
  end
@@ -0,0 +1,54 @@
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(:redis_store) do
19
+ ActiveSupport::Cache::RedisCacheStore.new(redis: $redis)
20
+ end
21
+
22
+ let(:brotli_store) do
23
+ RailsBrotliCache::Store.new(
24
+ redis_store
25
+ )
26
+ end
27
+
28
+ describe "generated cache keys are identical to standard cache stores" do
29
+ it "works for string keys" do
30
+ redis_store.fetch("string-key") { 123 }
31
+ brotli_store.fetch("string-key") { 123 }
32
+ expect($redis.get("br-string-key")).to be_present
33
+ expect($redis.get("string-key")).to be_present
34
+ end
35
+
36
+ it "ActiveModel object keys" do
37
+ post_1 = Post.new(id: 1)
38
+ redis_store.fetch(post_1) { 123 }
39
+ brotli_store.fetch(post_1) { 123 }
40
+ expect($redis.get("br-post/1")).to be_present
41
+ expect($redis.get("post/1")).to be_present
42
+ end
43
+
44
+ it "ActiveModel objects complex collection keys" do
45
+ post_1 = Post.new(id: 1)
46
+ post_2 = Post.new(id: 2)
47
+ collection = [post_1, post_2]
48
+ redis_store.fetch([:views, "controller/action", collection]) { 123 }
49
+ brotli_store.fetch([:views, "controller/action", collection]) { 123 }
50
+ expect($redis.get("views/controller/action/post/1/post/2")).to be_present
51
+ expect($redis.get("br-views/controller/action/post/1/post/2")).to be_present
52
+ end
53
+ end
54
+ 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.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-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