Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have the requirement that I need to map the start of (what is effectively) PATH_INFO to an int (PK on some table). I had a look at Apache RewriteMap and chose to go down that route. I'm not sure what go-faster techniques are valid in this environment, or whether they would have any impact (e.g. memoize).

Using files to lookup the values, RewriteMap only handles simple string comparisons, which does not cater for my need.

These are my test URLs:
http://myurl.com/aa/index.html http://myurl.com/aa/bb/index.html http://myurl.com/aa/foo/index.html http://myurl.com/cc/index.html http://myurl.com/cc/dd/index.html
This is my mapfile:
aa 1 bb 2 cc/dd 3 cc/ee 4
Consider using the following mapping:
RewriteMap mapping txt:mapfile.txt RewriteRule ^/(.*)/ - [E=TEST_VAL:$1,E=TEST_RESULT:${mapping:$1},NS]

Because Apache uses a simple text match, the '/'s (slashes) in the URLs break the mapping. Which led me to use a program.

Apache Config: RewriteMap mapping prg:/my/dir/mapper.pl

mapper.pl:

#!/usr/bin/perl my $CONFIG_FILE = 'mapfile.txt'; my $config = &get_config; $| = 1; while(<STDIN>){ my $pk = lookup($_); print "$pk\n"; } sub get_config { my $apache_boot_time_message = "$0: Setting up Apache RewriteMap [$C +ONFIG_FILE]"; print STDERR "$apache_boot_time_message\n"; my %config; open( CONFIG, '<', $CONFIG_FILE ) or die ("Could not open $CONFIG_FI +LE: $!"); while(<CONFIG>){ chomp; my $line = $_; my @line = split('\s+', $line); $config{$line[0]} = $line[1] unless $line =~ /^#/; } close CONFIG; return \%config; } sub lookup { chomp; my $DIR = shift; my @parts = split( '/', $DIR ); for( 0..$#parts ){ my $dir = join('/', @parts[0..$_]); return $config->{$dir} if $config->{$dir}; } return "NULL"; }

As this script is going to be hit so many times by the webserver, I wondered if anyone had ideas on speeding it up?

-=( Graq )=-


In reply to Speeding up Apache RewriteMap script. by graq

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-19 16:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found