Hello,
I have encountered a probable bug/problem with the caching of templates in
Velocity. The effect of the bug is that I have to request a page two times
after I have made a change to a template before the changes
get visible and Velocity uses the changes I made in the template.
After checking my own TemplateLoader for errors and not finding any
there (i think :)) I checked the source code to find out what really happend.
And what I found was that in the method requiresChecking() of the Resource
class a check is made wheater to check for changes in a specific template.
...
if ( lastCheck >= nextCheck)
{
return true;
}
else
{
lastCheck = System.currentTimeMillis();
return false;
}
...
When I first load a template the lastCheck variable is set to current
time and nextCheck to the same plus the modificationCheckInterval by the
touch() method. When I request the template the second time (later
than the time specified by modificationCheckInterval) the
requiresChecking() checks using the code above. It returns the result
that checking is not needed because lastCheck is still lesser than
nextCheck. So the else part is executed and lastCheck is assigned the
current time. This is the problem. The time it should check against is
not the lastChecked time but the current time. If I then make a request
again and the check is made it will return true to check if the template
has been modified. Then the touch() method is called again and it all
starts over.
So I made some changes and I got it working.
requiresChecking() method:
public boolean requiresChecking()
{
/*
* short circuit this if modificationCheckInterval == 0
* as this means "don't check"
*/
if (modificationCheckInterval <= 0 )
{
return false;
}
/*
* otherwise, see where we are
*/
if ( System.currentTimeMillis() >= nextCheck)
{
return true;
}
else
{
return false;
}
}
touch() method:
public void touch()
{
nextCheck = System.currentTimeMillis() + ( MILLIS_PER_SECOND *
modificationCheckInterval);
}
Have you encountered this bug/problem too? Or is it supposed to be like
that.
I am using the velocity-1.1.jar
I have attached a diff as well.
Thanks for a great product!
Andreas Wikberger
S p i r o k o m m u n i k a t i o n A B
Stena Center 1C
412 92 G�teborg
phone +46 (0)31 - 772 80 73
mobile +46 (0)705 - 18 77 48
[EMAIL PROTECTED]
www.spiro.se
165a166
>
180c181
< if ( lastCheck >= nextCheck)
---
> if ( System.currentTimeMillis() >= nextCheck)
186d186
< lastCheck = System.currentTimeMillis();
197,198c197
< lastCheck = System.currentTimeMillis();
< nextCheck = lastCheck + ( MILLIS_PER_SECOND * modificationCheckInterval);
---
> nextCheck = System.currentTimeMillis() + ( MILLIS_PER_SECOND *
>modificationCheckInterval);