File Coverage

lib/Email/MIME/CreateHTML/Resolver/Cached.pm
Criterion Covered Total %
statement 28 28 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 37 37 100.0


line stmt bran cond sub pod time code
1             ###############################################################################
2             # Purpose : Apply caching to another resolver
3             # Author : John Alden
4             # Created : Aug 2006
5             ###############################################################################
6              
7             package Email::MIME::CreateHTML::Resolver::Cached;
8              
9 2     2   11 use strict;
  2         5  
  2         81  
10 2     2   2307 use Data::Serializer;
  2         8093  
  2         78  
11 2     2   2055 use URI::Escape;
  2         4098  
  2         876  
12              
13             our $VERSION = '1.040';
14              
15             sub new {
16 4     4 1 9 my ($class, $args) = @_;
17 4         23 my $self = {
18             'Resolver' => $args->{resolver},
19             'Cache' => $args->{object_cache},
20             'base' => $args->{base},
21             };
22 4         18 return bless($self, $class);
23             }
24              
25             sub get_resource {
26 4     4 1 7 my ($self, $uri) = @_;
27 4         29 my $args = {'uri' => $uri, 'base' => $self->{base}, 'resolver' => ref $self->{Resolver}};
28 4         26 my $key = join('&', map {$_ . '=' . URI::Escape::uri_escape($args->{$_})} grep {defined $args->{$_}} sort(keys %$args));
  8         226  
  12         29  
29 4         54 my $cache = $self->{Cache};
30 4         17 my $serialized = $cache->get( $key );
31 4         60 my $ds = Data::Serializer->new();
32 4         10101 my @rv;
33 4 100       13 if ( defined $serialized ) {
34 2         9 my $deserialized = $ds->deserialize( $serialized );
35 2         474 @rv = @$deserialized;
36             }
37             else {
38 2         12 @rv = $self->{Resolver}->get_resource( $uri );
39 2         11 my $serialized = $ds->serialize( \@rv );
40 2         437 $cache->set( $key,$serialized );
41             }
42 4         78 return @rv;
43             }
44              
45             1;
46              
47             =head1 NAME
48              
49             Email::MIME::CreateHTML::Resolver::Cached - wraps caching around a resource resolver
50              
51             =head1 SYNOPSIS
52              
53             my $o = new Email::MIME::CreateHTML::Resolver::Cached(\%args)
54             my ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri)
55              
56             =head1 DESCRIPTION
57              
58             This is used by Email::MIME::CreateHTML to load resources.
59              
60             =head1 METHODS
61              
62             =over 4
63              
64             =item $o = new Email::MIME::CreateHTML::Resolver::Cached(\%args)
65              
66             %args can contain:
67              
68             =over 4
69              
70             =item base
71              
72             Base URI to resolve URIs passed to get_resource.
73              
74             =item object_cache (mandatory)
75              
76             A cache object
77              
78             =item resolver (mandatory)
79              
80             Another resolver to apply caching to
81              
82             =back
83              
84             =item ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri)
85              
86             =back
87              
88             =head1 AUTHOR
89              
90             Tony Hennessy, Simon Flack and John Alden with additional contributions by
91             Ricardo Signes and Henry Van Styn
92              
93             =head1 COPYRIGHT
94              
95             (c) BBC 2005,2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
96              
97             See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
98              
99             =cut