Hello,

I am using Cache::FastMmap 1.34 with mod_perl 2.0.4 (Debian packaged) to help with configuration. I am using the PerlTransHandler hook to determine where on the filesystem a particular virtual host resides, by looking it up in a database then caching the results.

It mostly works, but every now and then it stops working. When it's not working Apache seems to hang on every page load, and I see this in the log:

[Thu Nov 12 23:30:56 2009] [notice] child pid 21117 exit signal Alarm +clock (14) [Fri Nov 13 00:14:54 2009] [notice] child pid 21309 exit signal Alarm +clock (14) [Fri Nov 13 00:49:55 2009] [error] [client 66.249.71.244] cache old sl +ots mistmatch at /usr/local/lib/perl/5.10.0/Cache/FastMmap.pm line 65 +6.\n [Fri Nov 13 01:46:04 2009] [error] [client 68.40.244.40] cache old slo +ts mistmatch at /usr/local/lib/perl/5.10.0/Cache/FastMmap.pm line 656 +.\n

Google doesn't turn up any obvious solutions for this.

My Apache configuration looks like this:

PerlModule My::VirtualHost PerlTransHandler My::VirtualHost::TransHandler

My code looks basically like this (in reality it is a bit more complicated):

package My::VirtualHost::TransHandler; use strict; use warnings; use Apache2::Const -compile => qw(OK DECLINED); use Apache2::RequestRec (); use My::Config; use DBI; use Cache::FastMmap; # This happens globally in the parent process our $cache = Cache::FastMmap->new( read_cb => \&get_virthost, expire_time => '1m', init_file => 1, cache_not_found => 1, ); our($dbh,$sth); sub get_sth { if (!$dbh) { $dbh = DBI->connect(My::Config::DB_DSN, My::Config::DB_USER, M +y::Config::DB_PASS) or die "Couldn't connect to database\n"; $sth = $dbh->prepare('SELECT virthost_id FROM virtual_hosts WH +ERE url = lower(?)') or die "Couldn't prepare statement\n"; } return $sth; } sub get_virthost { my($ctx,$hn)=@_; my $ret; eval { my $url = 'http://'.$hn; my $sth = get_sth(); $sth->execute($url); my $row = $sth->fetchrow_arrayref(); if ($row && defined($row->[0])) { $ret = $row->[0]; } else { $ret = undef; } $sth->finish(); return $ret; }; if ($@) { # TODO: Some kind of error handling warn "Error getting virtual host: $@"; $ret = undef; } $ret; } sub handler { my $r = shift; my $hn = $r->hostname(); if (!$hn) { # TODO: Return some kind of error return Apache2::Const::DECLINED; } my $id = $cache->get($hn); if (defined($id)) { my $newfn = My::Config::BASE_PATH . "/virthosts/$id".$r->uri() +; $r->filename($newfn); return Apache2::Const::OK; } else { return Apache2::Const::DECLINED; } }

My questions are:

Thanks!


In reply to Cache::FastMmap and mod_perl by sgifford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.