Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 680
deruse Avatar asked Oct 22 '25 22:10

deruse


1 Answers

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
like image 187
rewritten Avatar answered Oct 24 '25 13:10

rewritten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!