-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnginx_test.pl
executable file
·75 lines (65 loc) · 1.92 KB
/
nginx_test.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use DBI;
use DBD::Sybase;
use Cache::Memcached;
use Log::Log4perl;
#init logger
Log::Log4perl->init("./log4perl.conf");
#declare memcached server list
my @memcached_servers = ("localhost:11211");
#declare sqlserver
my $database="ComBeziPic";
my $dsn = "dbi:Sybase:server=fastdfs;database=$database";
my $user="writeuser";
my $auth="write\@520";
sub urlhandler {
#init logger
my $logger = Log::Log4perl->get_logger();
#my $r = shift;
#my $url = $r->uri;
my $url = "http://pic11.shangpin.com/e/s/15/04/17/20100428112749234441-960-470.jpg";
#1.reg test url rule
if($url =~ /^http:\/\/.+?(\/[a-z]{1}\/[a-z]{1}\/)\d{2}\/\d{2}\/\d{2}\/(\d{20})\-(\d{3})\-(\d{3})\.(jpg|png)$/){
# print "$1\n";
# print "$2\n";
# print "$3\n";
# print "$4\n";
# print "$5\n";
# $logger->info("test true");
#2.test true, first from memcached get cached url and redirect
my $cached_key = "fastdfs_".$1."_".$2;
my $cache = Cache::Memcached->new(servers => [@memcached_servers]);
my $real_url = $cache->get($cached_key);
if($real_url) {
print $real_url->{'FilePath0'}."\n";
return $real_url->{'FilePath0'};
}
#get sqlserver url monitor real url
my $table_name;
#get sqlserver table name
if($1 eq "/f/p") {
$table_name = "ProductPic";
} elsif ($1 eq "/e/u") {
$table_name = "UserPic";
} elsif($1 eq "/e/s/") {
$table_name = "ProductPic";
}
print "$table_name\n";
my $sql = "select * from $table_name where PictureFileNo = '$2'";
#declare sqlserver conn
my $dbh = DBI->connect($dsn, $user, $auth, {RaiseError => 1, AutoCommit => 1})
|| die "Database connection not made: $DBI::errstr";
my $item = $dbh->selectrow_hashref($sql);
$dbh->disconnect;
#add cached to memcached
$cache->set($cached_key, $item, 3600 * 24 * 30);
$cache->disconnect_all;
return $item->{'FilePath0'};
} else {
$logger->info("test false");
return;
}
}
print urlhandler()."\n";