Reputation: 11631
Is there a way to do something like
Rails.cache.fetch("id..", expire_in: 1.day, expire_at: midnight) do
#...
end
thanks!
Upvotes: 7
Views: 4045
Reputation: 662
Careful not to get a decimal, if you want an integer...
>_ bundle exec rails console
Loading development environment (Rails 6.0.3.2)
>> Time.now.end_of_day + 1.day - Time.now
169207.001996682
>> (Time.now.end_of_day + 1.day - Time.now).to_i
169204
>>
Upvotes: 0
Reputation: 35
you can set expire time when you are creating it
Rails.cache.write("your key or id", expires_in: 1.day)
Upvotes: 0
Reputation: 124419
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
.
Upvotes: 12