I have a problem with caching method results in rails project.
I came to this code that simulates two different requests:
[
Thread.new { puts Rails.cache.fetch('a', expires_in: 2.seconds) { rand } },
Thread.new { puts Rails.cache.fetch('a', expires_in: 2.seconds) { rand } }
]
Try to execute it in a rails console. If I try to run it in a console as output I get different numbers. How is that possible?
But for example this code (with sleep) works as intended:
[
Thread.new { sleep(1); puts Rails.cache.fetch('a', expires_in: 2.seconds) { rand } },
Thread.new { puts Rails.cache.fetch('a', expires_in: 2.seconds) { rand } }
]
You could use mutex to prevent running some code at the same time:
In the 1st block of code
I think your rails configuration is using file for caching (to ensure that, pls check the folder /tmp/cache in your rails app).
Because 'Rails.cache' needs to perform reading cache from file, it is time-consuming, and the action 'fetch' in 2 threads mostly run at the same time, so 'a' value doesn't exist or is expired already.
In the 2nd block of code
When the first thread tries to fetch 'a', 'a' is in cache file already. So that is why it works.
Let make another simulation, instead of using 'Rails.cache', we will use MemoryStore cache like this:
It works as our expectation as well!