使用Rails和Postgres获取原子计数器(自增)的值

5

我需要原子方式增加一个模型计数器并使用其新值(由Sidekiq作业处理)。

目前,我使用

Group.increment_counter :tasks_count, @task.id

在我的模型中,我会自动递增计数器的值。
但是,如果计数器的值达到了50,我还需要发送一个通知,有什么好的想法吗?锁定表/行或者有更简单的方法吗?
编辑/解决方案
基于mu is too short的回答和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在同一查询中获取新值。
1个回答

5

您的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

我不知道有没有便利的 Rails 方法来将该 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

谢谢您指引我。根据您的回答,我更新了我的问题,并添加了一个实例方法。 - tbuehl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接