Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 使用CGI脚本作为具有缓存功能的图像提供程序_Perl_Caching_Cgi - Fatal编程技术网

Perl 使用CGI脚本作为具有缓存功能的图像提供程序

Perl 使用CGI脚本作为具有缓存功能的图像提供程序,perl,caching,cgi,Perl,Caching,Cgi,我试图使用CGI脚本作为我的应用程序资产的静态提供者。因为我对Perl比较陌生,所以我认为我的代码不是实现我想要的东西的最佳方式。如果你有更好的代码,让我知道。这是我最近的剧本 #!/usr/bin/perl use lib "/usr/share/perl/5.10.1"; $asset_path = "/home/foo/public_html/assets/img/"; $uri_asset_path = "http://v2.foo.com/assets/img/"; use File:

我试图使用CGI脚本作为我的应用程序资产的静态提供者。因为我对Perl比较陌生,所以我认为我的代码不是实现我想要的东西的最佳方式。如果你有更好的代码,让我知道。这是我最近的剧本

#!/usr/bin/perl
use lib "/usr/share/perl/5.10.1";
$asset_path = "/home/foo/public_html/assets/img/";
$uri_asset_path = "http://v2.foo.com/assets/img/";
use File::stat;
use Time::localtime;
$ENV{QUERY_STRING}=~ s/:/\//g;
$file = $asset_path . $ENV{QUERY_STRING};
unless (-e $file) {
print header('text/html','404 Not Found'); 
exit();
} 
$raw_last_modified = ctime(stat($file)->mtime);
$age = 30*24*60*60;
$date_day = substr $raw_last_modified, 0, 3;
$date_mon = substr $raw_last_modified, 4, 3;
$date_mon = substr $raw_last_modified, 4, 3;
$date_d = substr $raw_last_modified, 8, 2;
$date_time = substr $raw_last_modified, 11, 8;
$date_year = substr $raw_last_modified, 20, 4;
$last_modified = "$date_day, $date_d $date_mon $date_year $date_time GMT";
use CGI qw/:standard/;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $response = $ua->get("$uri_asset_path/$ENV{QUERY_STRING}");
unless ($response->is_success){
    print header('text/html','404 Not Found'); 
    exit();
}
if(defined($ENV{'HTTP_IF_MODIFIED_SINCE'})){
    if ($ENV{'HTTP_IF_MODIFIED_SINCE'} == $last_modified){
        print header('text/html','304 Not Modified');
        return();
    } 
}
print header(-type=>'image/png',-expires=>'+30d',-last_modified => $last_modified,-cache_control => 'max-age='.$age.', must-revalidate');
print $response->content;

谢谢。

您似乎没有具体的问题。我假设您正在征求关于如何正确编写CGI脚本的建议

我不明白为什么需要为此编写CGI脚本。只需正确配置您的web服务器以添加
expires
和信息

您应该添加
使用警告
使用严格
到您的脚本,在“污染”模式下运行它,不要直接使用
$ENV{QUERY_STRING}
,而是使用
CGI.pm
param
方法等访问提供的变量

我不明白为什么出现了
LWP::UserAgent
,为什么要发出
GET

也许你可以解释你为什么要做你正在做的事情,并根据这个网站的问答性质提出一个具体的问题


此外,您可能需要阅读。

首先,感谢您的回复。我很感兴趣,与您的陈述相关:“我不明白为什么有必要为此编写CGI脚本”,因为在我尝试将它们转换为perl脚本之前,我是使用PHP来完成这项任务的。我是怎么想的,如果我使用perl脚本,我可以用php获得更高的性能(我也计划使用FastCGI)。我正在调试我的V2应用程序,并且会考虑所有的选项来最大化性能,每一个毫秒都能最大化我的应用程序性能。这是我的目标。确保在web服务器配置中而不是在CGI脚本中更好地处理图像缓存。我知道。此外,我意识到在某些时候,使用CDN是实现性能最大化的更好选择,但我还没有准备好(在预算中)。我的应用程序包括一些不能(也不应该)缓存的静态资源,比如用户图片、用户自定义css和其他用户内容。所以我的REST服务应该提供一个实际的或最新的资源,另一方面,其他静态资源可以被正确地缓存。既然你看起来像个Perl极客,你真的认为使用一些Perl脚本对我的应用程序性能没有任何影响吗?对于你的问题,为什么我使用GET,只是为了引用静态文件。简而言之,在我的css中,例如,现在我有这样的东西来引用图像
http://v2.foo.com/cgi-bin/static.pl?icon:main:event.png
从我的资产中实际获取event.pngpath@toopay但是脚本中的
GET
意味着您的脚本将创建一个
INET
套接字以连接到web服务器,读取整个响应,然后将其发送回客户端。首先,这需要时间。其次,它需要内存。第三,您甚至没有
binmode
STDOUT
。我不明白为什么这比配置web服务器更好。