Skip to content

Commit

Permalink
Fix paranoia soft delete record still exits bug
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Aug 19, 2019
1 parent aa3ba40 commit de65f1e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/second_level_cache/mixin.rb
Expand Up @@ -71,6 +71,9 @@ def expire_second_level_cache

def write_second_level_cache
return unless klass.second_level_cache_enabled?
# Avoid rewrite cache again, when record has been soft deleted
return if self.respond_to?(:deleted?) && self.send(:deleted?)

marshal = RecordMarshal.dump(self)
expires_in = klass.second_level_cache_options[:expires_in]
expire_changed_association_uniq_keys
Expand Down
7 changes: 5 additions & 2 deletions test/base_test.rb
Expand Up @@ -8,7 +8,7 @@ def setup
end

def test_should_update_cache_when_update_attributes
@user.update_attributes name: "change"
@user.update! name: "change"
assert_equal @user.name, User.read_second_level_cache(@user.id).name
end

Expand All @@ -18,13 +18,16 @@ def test_should_update_cache_when_update_attribute
end

def test_should_expire_cache_when_destroy
@user = User.create name: "csdn", email: "test@csdn.com"
@user.destroy
assert_nil User.find_by_id(@user.id)
assert_nil SecondLevelCache.cache_store.read(@user.second_level_cache_key)
assert_nil User.read_second_level_cache(@user.id)
end

def test_should_expire_cache_when_update_counters
assert_equal 0, @user.books_count
@user.books.create
@user.books.create!
assert_nil User.read_second_level_cache(@user.id)
user = User.find(@user.id)
assert_equal 1, user.books_count
Expand Down

0 comments on commit de65f1e

Please sign in to comment.