ruby-on-rails – 使用Rails和Postgres获取原子计数器(增量)的值
发布时间:2020-12-17 03:18:05 所属栏目:百科 来源:网络整理
导读:我需要以原子方式递增模型计数器并使用其新值(由Sidekiq作业处理). 目前,我使用 Group.increment_counter :tasks_count,@task.id 在我的模型中,原子地递增计数器. 但是,如果计数器有例如,我也需要它的新值来发送通知.价值50.任何想法?锁定桌子/行还是有更简
我需要以原子方式递增模型计数器并使用其新值(由Sidekiq作业处理).
目前,我使用 Group.increment_counter :tasks_count,@task.id 在我的模型中,原子地递增计数器. 但是,如果计数器有例如,我也需要它的新值来发送通知.价值50.任何想法?锁定桌子/行还是有更简单的方法? 编辑/解决方案 基于mu太短的答案和Rails的update_counters方法,我实现了一个实例方法(用PostgreSQL测试). def self.increment_counter_and_return_value(counter_name,id) quoted_column = connection.quote_column_name(counter_name) quoted_table = connection.quote_table_name(table_name) quoted_primary_key = connection.quote_column_name(primary_key) quoted_primary_key_value = connection.quote(id) sql = "UPDATE #{quoted_table} SET #{quoted_column} = COALESCE(#{quoted_column},0) + 1 WHERE #{quoted_table}.#{quoted_primary_key} = #{quoted_primary_key_value} RETURNING #{quoted_column}" connection.select_value(sql).to_i end 使用它像: Group.increment_counter_and_return_value(:tasks_count,@task.id) 它使用RETURNING在同一查询中获取新值. 解决方法
您的Group.increment_counter调用将这样的SQL发送到数据库:
update groups set tasks_count = coalesce(tasks_counter,0) + 1 where id = X 其中X是@ task.id.获取新的tasks_counter值的SQL方法是包含RETURNING子句: update groups set tasks_count = coalesce(tasks_counter,0) + 1 where id = X returning tasks_count 我不知道有什么方便的Railsy方法来获取SQL到数据库.通常的Rails方法是要么做一堆锁定并重新加载@task或跳过锁定并希望最好: Group.increment_counter :tasks_count,@task.id @task.reload # and now look at @task.tasks_count to get the new value 你可以像这样使用RETURNING: new_count = Group.connection.execute(%Q{ update groups set tasks_count = coalesce(tasks_counter,0) + 1 where id = #{Group.connection.quote(@task.id)} returning tasks_count }).first['tasks_count'].to_i 你可能想隐藏在Group上的方法背后的那个烂摊子,这样你就可以说: n = Group.increment_tasks_count_for(@task) # or n = @task.increment_tasks_count (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |