【问题标题】:Is Rails.cache.fetch method atomic?Rails.cache.fetch 方法是原子的吗?
【发布时间】:2013-03-19 01:08:34
【问题描述】:

具体来说,如果缓存中不存在该键,我想将键值对添加到缓存中。这可以使用 Rails.cache.fetch 以原子方式完成吗?

谢谢!

【问题讨论】:

  • 您使用的是哪个缓存选项?

标签: ruby-on-rails memcached


【解决方案1】:

您必须使用:unless_exist 选项写入:

Rails.cache.write(k, v, :unless_exist => true)

仅当键不存在时,该方法才会返回 true。否则,该方法返回 false:

if Rails.cache.write(k, v, :unless_exist => true)
  # do something only when the key wasn't there
end

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2010-12-11
    • 2020-05-07
    • 2013-01-26
    相关资源
    最近更新 更多