File Coverage

blib/lib/Plack/Middleware/Static/OpenFileCache.pm
Criterion Covered Total %
statement 45 56 80.3
branch 14 20 70.0
condition 4 9 44.4
subroutine 9 11 81.8
pod 1 1 100.0
total 73 97 75.2


line stmt bran cond sub pod time code
1             package Plack::Middleware::Static::OpenFileCache;
2              
3 3     3   46409 use 5.008005;
  3         13  
  3         114  
4 3     3   15 use strict;
  3         6  
  3         88  
5 3     3   23 use warnings;
  3         5  
  3         87  
6 3     3   2352 use parent qw/Plack::Middleware::Static/;
  3         838  
  3         16  
7 3     3   462142 use Plack::Util::Accessor qw(max expires buf_size cache_errors);
  3         9  
  3         12  
8 3     3   2884 use Cache::LRU::WithExpires;
  3         20710  
  3         1232  
9              
10             our $VERSION = "0.02";
11              
12             sub prepare_app {
13 6     6 1 18907 my $self = shift;
14 6         38 my $max = $self->max;
15 6 50       300 $max = 100 unless defined $max;
16 6 100       20 $self->expires(60) unless defined $self->expires;
17 6 50       76 $self->buf_size(8192) unless defined $self->buf_size;
18 6         144 $self->{_cache_lru} = Cache::LRU::WithExpires->new(size => $max);
19             }
20              
21             sub _handle_static {
22 36     36   3071773 my($self, $env) = @_;
23 36         59 my $path = $env->{PATH_INFO};
24 36         69 my $cache = $self->{_cache_lru};
25 36         139 my $res = $cache->get($path);
26 36 100       551 if ( $res ) {
27 1 50       5 if ( ref $res->[2] ne 'ARRAY' ) {
28 0         0 seek($res->[2],0,0);
29             }
30 1         3 return $res;
31             }
32 35         135 $res = $self->SUPER::_handle_static($env);
33 35 100       4044 return unless defined $res;
34 11 100       49 if ( ref $res->[2] ne 'ARRAY' ) {
35 6         35 my $len = Plack::Util::header_get($res->[1], 'Content-Length');
36 6 50 33     350 if ( $self->{buf_size} && $len && $len < $self->{buf_size} ) {
      33        
37 6         31 local $/ = 65536;
38 6         247 my $buf = $res->[2]->getline;
39 6         428 $res->[2] = [$buf];
40             }
41             else {
42 0         0 my $io_path = $res->[2]->path;
43 0         0 bless $res->[2], 'Plack::Middleware::Static::OpenFileCache::IOWithPath';
44 0         0 $res->[2]->path($io_path);
45             }
46             }
47 11 100 66     184 if ( $res->[0] =~ m!^2! or $self->cache_errors ) {
48 6         33 $cache->set($path, $res, $self->expires);
49             }
50 11         358 $res;
51             }
52              
53             package Plack::Middleware::Static::OpenFileCache::IOWithPath;
54 3     3   26 use parent qw(IO::Handle);
  3         8  
  3         19  
55              
56             sub path {
57 0     0     my $self = shift;
58 0 0         if (@_) {
59 0           ${*$self}{+__PACKAGE__} = shift;
  0            
60             }
61 0           ${*$self}{+__PACKAGE__};
  0            
62             }
63              
64 0     0     sub close {}
65              
66             1;
67             __END__