I’m caching some html on a page like this:
<% cache do %>
<%= some_field %> minutes ago
<% end %>
I don’t want the db query to check that field (it’s that needed), but would still like the cache to expire maybe after 30 mins.
How long does it last by default? And what can be done to place an expiration on it?
What you’re doing is called Fragment Caching in Rails.
Rails Fragment caching will last until the first of any of the following happens
expire_fragmentYour cache store expiration depends on which store your app is using. If you’re using the file system, the cache could last for a very long time (days, weeks, etc).
Added If you use memcache as your store, memcache has an overall expire time. Plus memcache will expire items on a oldest touched, first expired basis if you limit the overall size of the memcache and it runs out of room when a new value is added.
Auto expiring A good method for expiring cache items is to store them in memcache and to automatically expire them when there’s fresh data. Do that by using version numbers in your cache keys. See The Secret to Memcached
Re: Expire after 30 minutes One way to do this would be to use memcache and set the overall expiration time to 30 minutes. Or you could have a cron job (or delay_job or other job runner) which would call expire_fragment every 30 minutes.