Does anyone know why rails seems to recalculate the value of "expires_in" each time a cached fragment is read?
cache("usecase_#{usecase.id}", :expires_in => "#{usecase.minutesToNextRun}".to_i.minutes) do
some code
end
If I check the logs I see that the usecase.minutesToNextRun
method is called each time the cache fragment is read (which is quite expensive and slows down the app).
Normally I'd expect rails to just calculate the :expires_in
value once and then just compare this value (e.g. 5 minutes) to the current time (in pseudocode).
if time_the_fragment_was_stored + expires_in > now
do nothing
else
re-render fragment
end
Does anyone have any hint/idea how I can prevent rails from recalculating the expiry time each time the fragment is read?
before this part of code (or in your controller or presenter):
cache("usecase_#{usecase.id}", :expires_in => "#{usecase.minutesToNextRun}".to_i.minutes) do
some code
end
try this:
@minutes_to_next_run ||= usecase.minutesToNextRun
in the example above, if @minutes_to_next_run
is nil
, then it is set equal to usecase.minutesToNextRun
. If it has a value, it uses it. If you read it symantically, it makes sense:
@minutes_to_next_run OR @minutes_to_next_run = usecase.minutesToNextRun
here is a (very) definitive answer about this