rails-brotli-cache 0.4.1 → 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: 01a0f784d05c6228ab9f4305fddda0b0622a3b0a1dfaba079e952c051064a535
4
- data.tar.gz: c12edd70df133be76fb09c2fdc179d83257307874d57f4f06fbe101fc639bc4a
3
+ metadata.gz: 52fe3544e2a3d65acf1bba0cd17939e62d628f1dcd6482ec7110c730494527a8
4
+ data.tar.gz: 9e3d9691e99051ce29ed22ccb3a7414e6a00c8037fbc877ef8892a4f4e2a2705
5
5
  SHA512:
6
- metadata.gz: 7a7cc8c87363e93b947bc606f1c0811199ff47ac574fd0bf329ef6248e6305661d0f305874c950bc4c02ee86327b2029c6d22ab01be7de81ab1e94af54d111cd
7
- data.tar.gz: 842e6e543337486dd313f65491ba9b56ca6fa15fcd8c94ed201d702d767d9ae41c777e240a6fd5791cbaa3d235f293a9d21281a76c8569271b7f1605b29377fe
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,26 +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
- def increment(*args)
132
- args[0] = cache_key(args[0])
133
- @core_store.increment(*args)
131
+ def increment(name, amount = 1, **options)
132
+ @core_store.increment(expanded_cache_key(name, options[:namespace]), amount, **options)
134
133
  end
135
134
 
136
- def decrement(*args)
137
- args[0] = cache_key(args[0])
138
- @core_store.decrement(*args)
135
+ def decrement(name, amount = 1, **options)
136
+ @core_store.decrement(expanded_cache_key(name, options[:namespace]), amount, **options)
139
137
  end
140
138
 
141
139
  def self.supports_cache_versioning?
@@ -176,8 +174,8 @@ module RailsBrotliCache
176
174
  Marshal.load(serialized)
177
175
  end
178
176
 
179
- def cache_key(name)
180
- "#{@prefix}#{name}"
177
+ def expanded_cache_key(name, namespace = nil)
178
+ "#{@prefix}#{::ActiveSupport::Cache.expand_cache_key(name, namespace)}"
181
179
  end
182
180
 
183
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.1"
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.1
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-05 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