Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails cache expire at midnight every day

Is there a way to do something like

Rails.cache.fetch("id..", expire_in: 1.day, expire_at: midnight) do
  #...
end

thanks!

like image 378
montrealmike Avatar asked Sep 20 '13 03:09

montrealmike


1 Answers

There isn't an expires_at option, but you can quickly calculate the number of seconds between your desired expiration time and the current time. Assuming you mean "expire at the end of the day tomorrow", you could do something like this:

expires_in_seconds = Time.now.end_of_day + 1.day - Time.now
Rails.cache.fetch("id...", expires_in: expires_in_seconds) do
  #...
end

Where expires_in_seconds would return a number of seconds (e.g. 90559)

If you just mean "end of today", it would be Time.now.end_of_day - Time.now.

like image 197
Dylan Markow Avatar answered Oct 12 '22 23:10

Dylan Markow