如何在perl脚本中缓存输出HTML?

时间:2014-01-27 06:47:35

标签: perl caching http-caching

任何可用于Cache输出html的Perl类? 数据库不需要在每次访问页面时执行大SQL查询

我为Cache找到了很多php类和函数,但在Perl中没有多少示例

我在Perl中看到这种类型Cache

http://codecanyon.net/item/autocache/1734137

请详细说明如何在Perl中进行操作

谢谢

1 个答案:

答案 0 :(得分:0)

这是一个简短的例子。重要的是,您需要对请求的URL进行少量检查。例如,url以小写形式完成,协议1位于url之前,类似于此类。一切都可以杀死你的md5。这只是一个简单的缓存方法。但它应该工作:)

未经过测试

use strict;
use warnings;

use Digest::MD5 qw(md5_hex);
use File::Slurp;

my $html = q{};
my $directory_to_md5 = 'directoy/to/md5sums/';
# this md5 comes from the requested url e.g.:
# its important, that the requested url is raw. you need to specify how it looks. for example, that the protocoll is always there.
# just implement a few checks
my $url_md5 = md5_hex('http://my-domain.com/test/call/etc');
# $url_md5 = 31330e57928d52c381e83355ea9e2aa5

if (-f $directory_to_md5 . $url_md5 ) {
  # read_file comes from File::Slurp
  # take a look further look at
  # http://p3rl.org/File::Slurp
  $html = read_file($directory_to_md5 . $url_md5);  
} else {
  # the real code goes here
  # ...
  $html = q{..}; # do stuff here.

  # write_file comes from File::Slurp as well
  write_file($directory_to_md5 . $url_md5, $html);
}

编辑:

我忘了说,你需要在else块中编写你的md5-cache-file