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
精彩评论