second_level_cache icon indicating copy to clipboard operation
second_level_cache copied to clipboard

缓存更新操作缺失锁,当同条记录高速并发更新的时候,写入的缓存可能是错误的。

Open OuYangJinTing opened this issue 5 years ago • 0 comments

user = User.create(name: "test", email: "[email protected]")
user1 = User.fetch_by_uniq_keys(id: user.id)
user2 = User.fetch_by_uniq_keys(id: user.id)

user1.update_attribute(:name, "user1")
user2.update_attribute(:email, "[email protected]")

user_from_cache = User.fetch_by_uniq_keys(id: user.id)
user_from_database = User.without_second_level_cache { User.find(user.id) }

assert_equal user_from_database.attributes, user_from_cache.attributes

------------------------------------------------------------------------------------------------------------------------------
bundle exec rake test result:
Failure:
FetchByUinqKeyTest#test_simulation_high_speed_update_same_record [/Code/second_level_cache/test/fetch_by_uniq_key_test.rb:111]:                                           
--- expected                                                                                                                                                              
+++ actual                                                                                                                                                                
@@ -1 +1 @@                                                                                                                                                               
-{"id"=>2, "options"=>[], "json_options"=>nil, "extras"=>{}, "name"=>"user1", "email"=>"[email protected]", "status"=>"active", "books_count"=>0, "images_count"=>0, "created_at"=>2021-01-29 06:42:26.331878 UTC, "updated_at"=>2021-01-29 06:42:28.335177 UTC}                                                                                      
+{"id"=>2, "options"=>[], "json_options"=>nil, "extras"=>{}, "name"=>"test", "email"=>"[email protected]", "status"=>"active", "books_count"=>0, "images_count"=>0, "created_at"=>2021-01-29 06:42:26.331878 UTC, "updated_at"=>2021-01-29 06:42:28.335177 UTC}

OuYangJinTing avatar Jan 29 '21 06:01 OuYangJinTing