开发者

Is Rails.cache.fetch method atomic?

开发者 https://www.devze.com 2023-04-13 01:28 出处:网络
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?

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

精彩评论

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

关注公众号