开发者

Is Rails.cache.fetch method atomic?

Specifically, if the key does not exist in the cache, I want to add the key-value pair to the cache开发者_开发知识库. Can this be done atomically using Rails.cache.fetch?

Thanks!


You have to write with the :unless_exist option:

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

The method will return true only when the key didn't exist. Otherwise, the method returns false:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜