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 } }
]