Reputation: 892
I'm trying to setup a caching system using the Memcached plugin with Rails 3.1 and dalli on Heroku cedar stack with a number of items that should expire at the end of each day.
For this I'm using a utility method:
def self.seconds_to_next_day
((DateTime.now.end_of_day - DateTime.now) * 24 * 60 * 60).to_i.seconds
end
And caching like so in haml views:
- cache "user_#{current_user.id}_my_groups", :expires_in => Utils.seconds_to_next_day do
= render "shared/my_groups"
But it's not expiring at the end of each day as it should. I have also tried passing in the number of seconds as an integer, without .seconds, but it made no difference.
For now I've fallen back to a scheduled task that runs daily at 00:00 and clears the day-sensitive cache items, but I'd much prefer to use native cache expiration.
Any ideas? Thanks!
Upvotes: 2
Views: 641
Reputation: 2951
In order to get your time calculation correctly on Rails and Heroku, you need to use Time and zone respectably. Thus, what you want is the following.
Time.zone.now.end_of_day - Time.zone.now
Upvotes: 2