| 1 | #file:Apache2/AMFMobileCachingMemcached.pm; | ||
| 2 | #-------------------------------- | ||
| 3 | |||
| 4 | # | ||
| 5 | # Created by Idel Fuschini | ||
| 6 | # Date: 01/08/10 | ||
| 7 | # Site: http://www.apachemobilefilter.org | ||
| 8 | # Mail: idel.fuschini@gmail.com | ||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | package Apache2::AMFMobileCachingMemcached; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use Apache2::AMFCommonLib (); | ||
| 17 | |||
| 18 | use Apache2::RequestRec (); | ||
| 19 | use Apache2::RequestUtil (); | ||
| 20 | use Apache2::SubRequest (); | ||
| 21 | use Apache2::Log; | ||
| 22 | use Apache2::Filter (); | ||
| 23 | use Apache2::Connection (); | ||
| 24 | use APR::Table (); | ||
| 25 | use LWP::Simple; | ||
| 26 | use Cache::Memcached; | ||
| 27 | use Apache2::Const -compile => qw(OK REDIRECT DECLINED); | ||
| 28 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; | ||
| 29 | use constant BUFF_LEN => 1024; | ||
| 30 | use vars qw($VERSION); | ||
| 31 | $VERSION= "4.33";;; | ||
| 32 | # | ||
| 33 | # Define the global environment | ||
| 34 | # | ||
| 35 | my $CommonLib = new Apache2::AMFCommonLib (); | ||
| 36 | my $SetCacheTime="900"; | ||
| 37 | my $serverMemCache; | ||
| 38 | my @Server; | ||
| 39 | $CommonLib->printLog("---------------------------------------------------------------------------"); | ||
| 40 | $CommonLib->printLog("AMFMobileCachingMemcached Version $VERSION"); | ||
| 41 | if ($ENV{AMFMobileHome}) { | ||
| 42 | &loadConfigFile(); | ||
| 43 | } else { | ||
| 44 | $CommonLib->printLog("AMFMobileHome not exist. Please set the variable AMFMobileHome into httpd.conf"); | ||
| 45 | $CommonLib->printLog("Pre-Requisite: WURFLFilter must be activated"); | ||
| 46 | ModPerl::Util::exit(); | ||
| 47 | } | ||
| 48 | if ($ENV{ServerMemCached}) { | ||
| 49 | $serverMemCache=$ENV{ServerMemCached}; | ||
| 50 | @Server = split(/,/, $ENV{ServerMemCached}); | ||
| 51 | $CommonLib->printLog("ServerMemCached is: $serverMemCache"); | ||
| 52 | } else { | ||
| 53 | $CommonLib->printLog("ServerMemCached is not setted. Please set the variable ServerMemCached into httpd.conf, example \"PerlSetEnv ServerMemCached 10.10.10.10:11211\""); | ||
| 54 | ModPerl::Util::exit(); | ||
| 55 | } | ||
| 56 | |||
| 57 | my $memd = new Cache::Memcached { | ||
| 58 | 'debug' => 0, | ||
| 59 | 'compress_threshold' => 10_000, | ||
| 60 | 'enable_compress' => 1, | ||
| 61 | }; | ||
| 62 | $memd->set_servers(\@Server); | ||
| 63 | |||
| 64 | sub loadConfigFile { | ||
| 65 | my $dummy; | ||
| 66 | my $carrier; | ||
| 67 | my $nation; | ||
| 68 | my $ip; | ||
| 69 | my $row; | ||
| 70 | my @rows; | ||
| 71 | my $carriernetdownload="none"; | ||
| 72 | my $carrierurl; | ||
| 73 | my $total_carrier_ip=0; | ||
| 74 | my $ip2; | ||
| 75 | |||
| 76 | $CommonLib->printLog("AMFCarrierDetection: Start read configuration from httpd.conf"); | ||
| 77 | if ($ENV{SetCacheTime}) { | ||
| 78 | $SetCacheTime=$ENV{SetCacheTime}; | ||
| 79 | $CommonLib->printLog("SetCacheTime is: $SetCacheTime (seconds)"); | ||
| 80 | } else { | ||
| 81 | $CommonLib->printLog("SetCacheTime is not setted. So, the default timeout is: $SetCacheTime seconds"); | ||
| 82 | } | ||
| 83 | |||
| 84 | $CommonLib->printLog("Finish loading parameter"); | ||
| 85 | } | ||
| 86 | sub handler { | ||
| 87 | my $f = shift; | ||
| 88 | my $r = $f->r; | ||
| 89 | my $finfo = $r->finfo; | ||
| 90 | my $uri = $f->r->uri(); | ||
| 91 | my $s = $f->r->server; | ||
| 92 | my $content_type=$f->r->content_type(); | ||
| 93 | my $hostname=$f->r->hostname(); | ||
| 94 | my $port=$f->r->get_server_port(); | ||
| 95 | my $id='null'; | ||
| 96 | my $query_string=$f->r->args; | ||
| 97 | if ($query_string) { | ||
| 98 | } else { | ||
| 99 | $query_string = ""; | ||
| 100 | } | ||
| 101 | $hostname="$hostname:$port"; | ||
| 102 | if ($f->r->pnotes('id')) { | ||
| 103 | $id=$f->r->pnotes('id') | ||
| 104 | } else { | ||
| 105 | $s->warn("AMF error - probably the AMFWURFLFilter is not started"); | ||
| 106 | } | ||
| 107 | unless( $f->ctx ) { | ||
| 108 | $f->r->headers_out->unset('Content-Length'); | ||
| 109 | $f->ctx(1); | ||
| 110 | } | ||
| 111 | my $key="$hostname:$id:$uri:$query_string"; | ||
| 112 | my $page=$f->read(my $buf, BUFF_LEN); | ||
| 113 | my $var=$memd->get($key); | ||
| 114 | if ($var) { | ||
| 115 | my $hash_dummy=$var; | ||
| 116 | my %hash=%$hash_dummy; | ||
| 117 | my $content_type = $hash{'content_type'}; | ||
| 118 | $buf = $hash{'page'}; | ||
| 119 | } else { | ||
| 120 | my %hash=('content_type',$content_type,'page',$buf); | ||
| 121 | $memd->set($key,\%hash, time + $SetCacheTime); | ||
| 122 | } | ||
| 123 | $f->r->content_type($content_type); | ||
| 124 | #$f->r->headers_out->set("Content-Length"=>$buf); | ||
| 125 | $f->print($buf); | ||
| 126 | return Apache2::Const::OK; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | |||
| 131 | |||
| 132 | =head1 NAME | ||
| 133 | |||
| 134 | Apache2::AMFMobileCachingMemcached - This module has the scope to cache the static in content in different layout for mobile. | ||
| 135 | |||
| 136 | =head1 DESCRIPTION | ||
| 137 | |||
| 138 | This module has the scope to cache the static in content in different layout for mobile. | ||
| 139 | |||
| 140 | For more details: http://wiki.apachemobilefilter.org | ||
| 141 | |||
| 142 | =head1 AMF PROJECT SITE | ||
| 143 | |||
| 144 | http://www.apachemobilefilter.org | ||
| 145 | |||
| 146 | =head1 DOCUMENTATION | ||
| 147 | |||
| 148 | http://wiki.apachemobilefilter.org | ||
| 149 | |||
| 150 | Perl Module Documentation: http://wiki.apachemobilefilter.org/index.php/AMFMobileCaching | ||
| 151 | |||
| 152 | =head1 AUTHOR | ||
| 153 | |||
| 154 | Idel Fuschini (idel.fuschini [at] gmail [dot] com) | ||
| 155 | |||
| 156 | =head1 COPYRIGHT | ||
| 157 | |||
| 158 | You may distribute under the terms of either the GNU General Public | ||
| 159 | License or the Artistic License, as specified in the Perl README file. | ||
| 160 | |||
| 161 | =cut |