second_level_cache 2.2.4 → 2.2.5

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
  SHA1:
3
- metadata.gz: 05c9987992ee5a153440093cb5790b0f3d1e64d0
4
- data.tar.gz: d8a274ec93948824185855cf1b8fb788aa844c34
3
+ metadata.gz: 468998db30f121cb2fe6759f3c7bc59beb70685f
4
+ data.tar.gz: efbb401b2975b3dc97a9cc639f3b1147e4c95332
5
5
  SHA512:
6
- metadata.gz: 58c4cb72d3ae8a74f39ab0a0a5dc71b44ba254d51e53e120f099486bdc7fdf0204434d069ebdbe2a30f66b3c90b9840d97ec6130599e98328fc00feef305ad33
7
- data.tar.gz: a32c661bbe6d71fabea69d3079a795ce9ec51315613fda3b26a1f163220c30f147b3c3c37cf843d0f113e0e2e41f5527b33aad5801af17a259f5c455e61b394a
6
+ metadata.gz: 2716b6392268aa3973b383994c87ba269feb5d59a420491ac3a61e334d3072c215fe9909ef242446aa7469bcd444a5262a830fb03af8b476245dce52da394a76
7
+ data.tar.gz: 66f5a460fd82d11a97dbc685410ea33a2de792c232aa3116eb3ccf50b36603ed4b209df16dede9fc8f3b099073396b51a895ede1cf5a1d02739a4eb6be9aa469
@@ -19,7 +19,7 @@ module SecondLevelCache
19
19
  record_marshals = RecordMarshal.load_multi(records_from_cache.values)
20
20
 
21
21
  if missed_ids.empty?
22
- return record_marshals
22
+ return SecondLevelCache::RecordRelation.new(record_marshals)
23
23
  end
24
24
 
25
25
  records_from_db = super(missed_ids)
@@ -27,7 +27,7 @@ module SecondLevelCache
27
27
  write_cache(r)
28
28
  end
29
29
 
30
- records_from_db + record_marshals
30
+ SecondLevelCache::RecordRelation.new(records_from_db + record_marshals)
31
31
  end
32
32
 
33
33
  private
@@ -0,0 +1,17 @@
1
+ module SecondLevelCache
2
+ class RecordRelation < Array
3
+ # A fake Array for fix ActiveRecord 5.0.1 records_for method changed bug
4
+ #
5
+ # in ActiveRecord 5.0.0 called:
6
+ # records_for(slice)
7
+ #
8
+ # but 5.0.1 called:
9
+ # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/preloader/association.rb#L118
10
+ # records_for(slice).load(&block)
11
+ #
12
+ # https://github.com/rails/rails/pull/26340
13
+ def load(&_block)
14
+ self
15
+ end
16
+ end
17
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module SecondLevelCache
3
- VERSION = '2.2.4'.freeze
3
+ VERSION = '2.2.5'.freeze
4
4
  end
@@ -1,6 +1,7 @@
1
1
  require 'active_support/all'
2
2
  require 'second_level_cache/config'
3
3
  require 'second_level_cache/record_marshal'
4
+ require 'second_level_cache/record_relation'
4
5
 
5
6
  module SecondLevelCache
6
7
  def self.configure
@@ -29,7 +30,11 @@ module SecondLevelCache
29
30
  end
30
31
 
31
32
  def second_level_cache_enabled?
32
- @second_level_cache_enabled == true
33
+ if defined? @second_level_cache_enabled
34
+ @second_level_cache_enabled == true
35
+ else
36
+ false
37
+ end
33
38
  end
34
39
 
35
40
  def without_second_level_cache
@@ -73,10 +78,19 @@ module SecondLevelCache
73
78
  return unless self.class.second_level_cache_enabled?
74
79
  marshal = RecordMarshal.dump(self)
75
80
  expires_in = self.class.second_level_cache_options[:expires_in]
81
+ expire_changed_association_uniq_keys
76
82
  SecondLevelCache.cache_store.write(second_level_cache_key, marshal, expires_in: expires_in)
77
83
  end
78
84
 
79
85
  alias update_second_level_cache write_second_level_cache
86
+
87
+ def expire_changed_association_uniq_keys
88
+ reflections = self.class.reflections.select { |_, reflection| reflection.belongs_to? }
89
+ changed_keys = reflections.map { |_, reflection| reflection.foreign_key } & previous_changes.keys
90
+ changed_keys.each do |key|
91
+ SecondLevelCache.cache_store.delete(self.class.send(:cache_uniq_key, key => previous_changes[key][0]))
92
+ end
93
+ end
80
94
  end
81
95
  end
82
96
 
@@ -33,4 +33,18 @@ class HasOneAssociationTest < ActiveSupport::TestCase
33
33
  clear_user = User.find(user.id)
34
34
  assert_equal clear_user.namespace.name, 'hooopo'
35
35
  end
36
+
37
+ def test_assign_relation
38
+ assert_equal @user.account, @account
39
+ new_account = Account.create
40
+ @user.account = new_account
41
+ assert_equal @user.account, new_account
42
+ assert_equal @user.reload.account, new_account
43
+ end
44
+
45
+ def test_belongs_to_column_change
46
+ assert_equal @user.account, @account
47
+ @account.update(user_id: @user.id + 1)
48
+ assert_nil @user.reload.account
49
+ end
36
50
  end
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.2.4
4
+ version: 2.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hooopo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-17 00:00:00.000000000 Z
11
+ date: 2016-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -145,6 +145,7 @@ files:
145
145
  - lib/second_level_cache/active_record/railtie.rb
146
146
  - lib/second_level_cache/config.rb
147
147
  - lib/second_level_cache/record_marshal.rb
148
+ - lib/second_level_cache/record_relation.rb
148
149
  - lib/second_level_cache/version.rb
149
150
  - second_level_cache.gemspec
150
151
  - test/active_record_test_case_helper.rb