second_level_cache 2.6.1 → 2.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +1 -1
- data/lib/second_level_cache/active_record.rb +7 -1
- data/lib/second_level_cache/active_record/base.rb +1 -5
- data/lib/second_level_cache/active_record/belongs_to_association.rb +3 -0
- data/lib/second_level_cache/active_record/fetch_by_uniq_key.rb +1 -1
- data/lib/second_level_cache/active_record/has_one_association.rb +1 -1
- data/lib/second_level_cache/active_record/preloader.rb +2 -1
- data/lib/second_level_cache/adapter/paranoia.rb +24 -0
- data/lib/second_level_cache/mixin.rb +0 -2
- data/lib/second_level_cache/version.rb +1 -1
- data/test/fetch_by_uniq_key_test.rb +6 -5
- data/test/model/paranoid.rb +10 -0
- data/test/model/user.rb +0 -2
- data/test/paranoid_test.rb +16 -0
- data/test/test_helper.rb +1 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84fecc39005beffa957b1602ff86ace6ca5419a1215d35011034ac6c78b5e0bf
|
4
|
+
data.tar.gz: f0a09a62d06ef9f10a1ee4559a4aac1b8e39eaa8a86ee81ce90d67212c9c1811
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40c406b9b4181c5e1085127bbab26b71c5a47842d685521b9fa1ec43e58e529d8591c24ae675760ed2a3dcc9644dca7fcb6db12a5e651b67670af413c16addcc
|
7
|
+
data.tar.gz: fc5f6ee2184884f4580123550e3d3edbb113114de4f7dacad340ef8c37711e9388335fdf029a06971fefe6a666ae0ca615f2852101384f986750333c3f85b0de
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -134,7 +134,7 @@ config.cache_store = [:dalli_store, APP_CONFIG["memcached_host"], { namespace: "
|
|
134
134
|
## Tips:
|
135
135
|
|
136
136
|
* When you want to clear only second level cache apart from other cache for example fragment cache in cache store,
|
137
|
-
you can only change the `cache_key_prefix
|
137
|
+
you can only change the `cache_key_prefix` (default: `slc`):
|
138
138
|
|
139
139
|
```ruby
|
140
140
|
SecondLevelCache.configure.cache_key_prefix = "slc1"
|
@@ -12,7 +12,13 @@ require "second_level_cache/active_record/preloader"
|
|
12
12
|
|
13
13
|
# http://api.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html
|
14
14
|
# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
|
15
|
-
ActiveSupport.on_load(:active_record) do
|
15
|
+
ActiveSupport.on_load(:active_record, run_once: true) do
|
16
|
+
if (Bundler.definition.gem("paranoia") rescue false)
|
17
|
+
require "second_level_cache/adapter/paranoia"
|
18
|
+
include SecondLevelCache::Adapter::Paranoia::ActiveRecord
|
19
|
+
SecondLevelCache::Mixin.send(:prepend, SecondLevelCache::Adapter::Paranoia::Mixin)
|
20
|
+
end
|
21
|
+
|
16
22
|
include SecondLevelCache::Mixin
|
17
23
|
prepend SecondLevelCache::ActiveRecord::Base
|
18
24
|
extend SecondLevelCache::ActiveRecord::FetchByUniqKey
|
@@ -6,11 +6,7 @@ module SecondLevelCache
|
|
6
6
|
def self.prepended(base)
|
7
7
|
base.after_commit :update_second_level_cache, on: :update
|
8
8
|
base.after_commit :write_second_level_cache, on: :create
|
9
|
-
|
10
|
-
base.after_destroy :expire_second_level_cache
|
11
|
-
else
|
12
|
-
base.after_commit :expire_second_level_cache, on: :destroy
|
13
|
-
end
|
9
|
+
base.after_commit :expire_second_level_cache, on: :destroy
|
14
10
|
|
15
11
|
class << base
|
16
12
|
prepend ClassMethods
|
@@ -6,6 +6,9 @@ module SecondLevelCache
|
|
6
6
|
module BelongsToAssociation
|
7
7
|
def find_target
|
8
8
|
return super unless klass.second_level_cache_enabled?
|
9
|
+
return super if klass.default_scopes.present? || reflection.scope
|
10
|
+
return super if reflection.active_record_primary_key.to_s != klass.primary_key
|
11
|
+
|
9
12
|
cache_record = klass.read_second_level_cache(second_level_cache_key)
|
10
13
|
if cache_record
|
11
14
|
return cache_record.tap { |record| set_inverse_instance(record) }
|
@@ -6,7 +6,7 @@ module SecondLevelCache
|
|
6
6
|
module HasOneAssociation
|
7
7
|
def find_target
|
8
8
|
return super unless klass.second_level_cache_enabled?
|
9
|
-
return super if reflection.scope
|
9
|
+
return super if klass.default_scopes.present? || reflection.scope
|
10
10
|
# TODO: implement cache with has_one scope
|
11
11
|
|
12
12
|
through = reflection.options[:through]
|
@@ -9,7 +9,8 @@ module SecondLevelCache
|
|
9
9
|
def records_for(ids, &block)
|
10
10
|
return super unless klass.second_level_cache_enabled?
|
11
11
|
return super unless reflection.is_a?(::ActiveRecord::Reflection::BelongsToReflection)
|
12
|
-
return super if klass.default_scopes.present?
|
12
|
+
return super if klass.default_scopes.present? || reflection.scope
|
13
|
+
return super if association_key_name.to_s != klass.primary_key
|
13
14
|
|
14
15
|
map_cache_keys = ids.map { |id| klass.second_level_cache_key(id) }
|
15
16
|
records_from_cache = ::SecondLevelCache.cache_store.read_multi(*map_cache_keys)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module SecondLevelCache
|
2
|
+
module Adapter
|
3
|
+
module Paranoia
|
4
|
+
module ActiveRecord
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
after_destroy :expire_second_level_cache
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Mixin
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
def write_second_level_cache
|
16
|
+
# Avoid rewrite cache again, when record has been soft deleted
|
17
|
+
return if respond_to?(:deleted?) && send(:deleted?)
|
18
|
+
super
|
19
|
+
end
|
20
|
+
alias update_second_level_cache write_second_level_cache
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -71,8 +71,6 @@ module SecondLevelCache
|
|
71
71
|
|
72
72
|
def write_second_level_cache
|
73
73
|
return unless klass.second_level_cache_enabled?
|
74
|
-
# Avoid rewrite cache again, when record has been soft deleted
|
75
|
-
return if respond_to?(:deleted?) && send(:deleted?)
|
76
74
|
|
77
75
|
marshal = RecordMarshal.dump(self)
|
78
76
|
expires_in = klass.second_level_cache_options[:expires_in]
|
@@ -6,15 +6,16 @@ class FetchByUinqKeyTest < ActiveSupport::TestCase
|
|
6
6
|
def setup
|
7
7
|
@user = User.create name: "hooopo", email: "hoooopo@gmail.com"
|
8
8
|
@post = Post.create slug: "foobar", topic_id: 2
|
9
|
+
@cache_prefix = SecondLevelCache.configure.cache_key_prefix
|
9
10
|
end
|
10
11
|
|
11
12
|
def test_cache_uniq_key
|
12
|
-
assert_equal User.send(:cache_uniq_key, name: "hooopo"), "uniq_key_User_name_hooopo"
|
13
|
-
assert_equal User.send(:cache_uniq_key, foo: 1, bar: 2), "uniq_key_User_foo_1,bar_2"
|
14
|
-
assert_equal User.send(:cache_uniq_key, foo: 1, bar: nil), "uniq_key_User_foo_1,bar_"
|
13
|
+
assert_equal User.send(:cache_uniq_key, name: "hooopo"), "#{@cache_prefix}/uniq_key_User_name_hooopo"
|
14
|
+
assert_equal User.send(:cache_uniq_key, foo: 1, bar: 2), "#{@cache_prefix}/uniq_key_User_foo_1,bar_2"
|
15
|
+
assert_equal User.send(:cache_uniq_key, foo: 1, bar: nil), "#{@cache_prefix}/uniq_key_User_foo_1,bar_"
|
15
16
|
long_val = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
16
|
-
assert_equal User.send(:cache_uniq_key, foo: 1, bar: long_val), "uniq_key_User_foo_1,bar_#{Digest::MD5.hexdigest(long_val)}"
|
17
|
-
assert Contribution.send(:cache_uniq_key, user_id: 1, date: Time.current.to_date), "uniq_key_Contribution_user_id_1,date_#{Time.current.to_date}"
|
17
|
+
assert_equal User.send(:cache_uniq_key, foo: 1, bar: long_val), "#{@cache_prefix}/uniq_key_User_foo_1,bar_#{Digest::MD5.hexdigest(long_val)}"
|
18
|
+
assert Contribution.send(:cache_uniq_key, user_id: 1, date: Time.current.to_date), "#{@cache_prefix}/uniq_key_Contribution_user_id_1,date_#{Time.current.to_date}"
|
18
19
|
end
|
19
20
|
|
20
21
|
def test_record_attributes_equal_where_values
|
data/test/model/user.rb
CHANGED
@@ -9,7 +9,6 @@ ActiveRecord::Base.connection.create_table(:users, force: true) do |t|
|
|
9
9
|
t.integer :status, default: 0
|
10
10
|
t.integer :books_count, default: 0
|
11
11
|
t.integer :images_count, default: 0
|
12
|
-
t.datetime :deleted_at
|
13
12
|
t.timestamps null: false, precision: 6
|
14
13
|
end
|
15
14
|
|
@@ -29,7 +28,6 @@ end
|
|
29
28
|
class User < ApplicationRecord
|
30
29
|
CACHE_VERSION = 3
|
31
30
|
second_level_cache(version: CACHE_VERSION, expires_in: 3.days)
|
32
|
-
acts_as_paranoid
|
33
31
|
|
34
32
|
serialize :options, Array
|
35
33
|
serialize :json_options, JSON if ::ActiveRecord::VERSION::STRING >= "4.1.0"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
class ParanoidTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
@paranoid = Paranoid.create
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_should_expire_cache_when_destroy
|
11
|
+
@paranoid.destroy
|
12
|
+
assert_nil Paranoid.find_by(id: @paranoid.id)
|
13
|
+
assert_nil SecondLevelCache.cache_store.read(@paranoid.second_level_cache_key)
|
14
|
+
assert_nil User.read_second_level_cache(@paranoid.id)
|
15
|
+
end
|
16
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: second_level_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.6.
|
4
|
+
version: 2.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hooopo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- lib/second_level_cache/active_record/has_one_association.rb
|
129
129
|
- lib/second_level_cache/active_record/persistence.rb
|
130
130
|
- lib/second_level_cache/active_record/preloader.rb
|
131
|
+
- lib/second_level_cache/adapter/paranoia.rb
|
131
132
|
- lib/second_level_cache/config.rb
|
132
133
|
- lib/second_level_cache/log_subscriber.rb
|
133
134
|
- lib/second_level_cache/mixin.rb
|
@@ -150,9 +151,11 @@ files:
|
|
150
151
|
- test/model/image.rb
|
151
152
|
- test/model/order.rb
|
152
153
|
- test/model/order_item.rb
|
154
|
+
- test/model/paranoid.rb
|
153
155
|
- test/model/post.rb
|
154
156
|
- test/model/topic.rb
|
155
157
|
- test/model/user.rb
|
158
|
+
- test/paranoid_test.rb
|
156
159
|
- test/persistence_test.rb
|
157
160
|
- test/polymorphic_association_test.rb
|
158
161
|
- test/preloader_belongs_to_test.rb
|
@@ -195,6 +198,7 @@ test_files:
|
|
195
198
|
- test/preloader_belongs_to_test.rb
|
196
199
|
- test/polymorphic_association_test.rb
|
197
200
|
- test/require_test.rb
|
201
|
+
- test/paranoid_test.rb
|
198
202
|
- test/finder_methods_test.rb
|
199
203
|
- test/preloader_has_one_test.rb
|
200
204
|
- test/preloader_non_integer_test.rb
|
@@ -213,6 +217,7 @@ test_files:
|
|
213
217
|
- test/model/topic.rb
|
214
218
|
- test/model/animal.rb
|
215
219
|
- test/model/order.rb
|
220
|
+
- test/model/paranoid.rb
|
216
221
|
- test/model/application_record.rb
|
217
222
|
- test/model/post.rb
|
218
223
|
- test/model/user.rb
|