Rails에서 카운터를 업데이트하면 동시성 문제가 발생합니까?
사람들이 항목에 투표 할 수있게하고 다음과 같이 말합니다.bid = Bid.new
..
bid.save!
item.total_bids += 1
item.save!
동시에 여러 사람이 동시에 항목을 묶는 경우 문제가 발생하지 않습니까?
Rails에서 카운터를 업데이트하면 동시성 문제가 발생합니까?
사람들이 항목에 투표 할 수있게하고 다음과 같이 말합니다.bid = Bid.new
..
bid.save!
item.total_bids += 1
item.save!
동시에 여러 사람이 동시에 항목을 묶는 경우 문제가 발생하지 않습니까?
절대적으로 병행 성 문제가있을 수 있습니다.
Item.increment_counter(:total_bids, item.id)
이 데이터베이스에 대한 SQL을 실행합니다 : 레일이 처리하는 increment_counter 제공
UPDATE items SET total_bids = total_bids + 1 WHERE id = x
자세한 내용은 여기를 참조하십시오 : 당신이 Sidekiq
를 사용하거나이있는 경우 http://api.rubyonrails.org/classes/ActiveRecord/CounterCache.html#method-i-increment_counter
비슷한 작업 대기열이있는 .delay
메소드를 사용해보십시오.
Item.delay.increment_counter(:total_bids, item.id)
카운트를 10으로 설정하거나 2 씩 감소 시키려면 어떻게합니까? – Blankman
그런 다음보다 일반적인 update_counters 루틴을 참조하십시오 : Item.update_counters item.id, : total_bids => -2 – asc99c