Thursday, November 8, 2007

my "caching" problem

Unable to drop it like a sane person when I went home, I was obsessing over my "caching problem." I did find the solution, and it was a caching issue. Sort of.

For those who weren't at the programmer's meeting last night, here's the mysterious behavior. When I change things on the User Preferences page (my version) and save them, the changes happen in the database but don't show up on a refresh of the page! Most mysterious. Immediate thought is "browser cache problem." Clear browser cache, close browser, reload browser, changes still haven't shown up. Restart apache... hey presto, changes showing up! Even curiouser, Aaron goes to the page and gets the info I'm seeing, prior to changes. It looks like one of Apache/mod_perl/HTML::Template/mysql is caching something somewhere.

My initial suspicion was that it was mod_perl doing it somehow. And you know what? I was right.

See this Perlmonks article.

We use Apache::Registry, of course, since it rules. Here's the relevant bits of code as it was when I was having the problem (or a reasonable facsimile):


my $page = CS3::Page->new();
my $user = $page->get_userX();



sub main {
# body of the script happens here, except that now I'm a closure over the
# initial instance of user that was here when this script was first compiled. D'oh!
# We should instantiate $page, $user, etc. here and pass them to our other
# subroutines...
}

main();


The problem with this code is described in the perlmonks article. Lesson learned: do not mix globals and subroutines inside Apache::Registry. Also, Apache::Registry is a bit of a hack, and it's best to be aware of how it does its magic, cause we use it. Everywhere.

No comments: