File: Cached.pm

package info (click to toggle)
libemail-mime-createhtml-perl 1.042-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 228 kB
  • sloc: perl: 632; makefile: 2
file content (99 lines) | stat: -rw-r--r-- 2,313 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
###############################################################################
# Purpose : Apply caching to another resolver
# Author  : John Alden
# Created : Aug 2006
###############################################################################

package Email::MIME::CreateHTML::Resolver::Cached;

use strict;
use Data::Serializer;
use URI::Escape;

our $VERSION = '1.042';

sub new {
	my ($class, $args) = @_;	
	my $self = {
		'Resolver' => $args->{resolver},
		'Cache'	=> $args->{object_cache},
		'base' => $args->{base},
	};
	return bless($self, $class);
}

sub get_resource {
	my ($self, $uri) = @_;
	my $args = {'uri' => $uri, 'base' => $self->{base}, 'resolver' => ref $self->{Resolver}};
	my $key = join('&', map {$_ . '=' . URI::Escape::uri_escape($args->{$_})} grep {defined $args->{$_}} sort(keys %$args));
	my $cache = $self->{Cache};
	my $serialized = $cache->get( $key );
	my $ds = Data::Serializer->new();
	my @rv;
	if ( defined $serialized ) {
	   my $deserialized = $ds->deserialize( $serialized );
	   @rv = @$deserialized;
	}
	else {
	   @rv = $self->{Resolver}->get_resource( $uri );
	   my $serialized = $ds->serialize( \@rv );
	   $cache->set( $key,$serialized );
	}
	return @rv;
}

1;

=head1 NAME

Email::MIME::CreateHTML::Resolver::Cached - wraps caching around a resource resolver

=head1 SYNOPSIS

	my $o = new Email::MIME::CreateHTML::Resolver::Cached(\%args)
	my ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri)

=head1 DESCRIPTION

This is used by Email::MIME::CreateHTML to load resources.

=head1 METHODS

=over 4

=item $o = new Email::MIME::CreateHTML::Resolver::Cached(\%args)

%args can contain:

=over 4

=item base

Base URI to resolve URIs passed to get_resource.

=item object_cache (mandatory)

A cache object

=item resolver (mandatory)

Another resolver to apply caching to

=back

=item ($content,$filename,$mimetype,$xfer_encoding) = $o->get_resource($uri)

=back

=head1 AUTHOR

Tony Hennessy, Simon Flack and John Alden with additional contributions by
Ricardo Signes <rjbs@cpan.org> and Henry Van Styn <vanstyn@cpan.org>

=head1 COPYRIGHT

(c) BBC 2005,2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL.

See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt

=cut