You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Andreas Wikberger <an...@blink.se> on 2001/07/26 10:28:19 UTC

Problems with the cache

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
andreas@spiro.se
www.spiro.se

Re: Problems with the cache

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Andreas Wikberger wrote:
> 
> 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.

Yep.  You are right.

[SNIP]

> 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.

That is now fixed in CVS HEAD.  Thanks for finding this and the fix.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

Re: Problems with the cache

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Yes, this is reasonable.  When my head clears, will take a look and get
this in.

geir

David Rees wrote:
> 
> > -----Original Message-----
> > From: Andreas Wikberger [mailto:andreas@blink.se]
> >
> > 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.
> 
> You know, the exact same bug has been bugging me for a while, too.  I looked
> at the source code briefly and didn't see anything wrong, but now I see that
> your analysis and fix is correct.
> 
> Geir, can get get this patch in the next release?  It would save me from
> hitting reload twice during development.  (I like to keep a 2s cache
> interval even on development machines).
> 
> -Dave

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

RE: Problems with the cache

Posted by David Rees <dr...@ebetinc.com>.
> -----Original Message-----
> From: Andreas Wikberger [mailto:andreas@blink.se]
>
> 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.

You know, the exact same bug has been bugging me for a while, too.  I looked
at the source code briefly and didn't see anything wrong, but now I see that
your analysis and fix is correct.

Geir, can get get this patch in the next release?  It would save me from
hitting reload twice during development.  (I like to keep a 2s cache
interval even on development machines).

-Dave