| 1 | ############################################################################### | ||
| 2 | # XML::Template::Cache | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@bbl.med.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Cache; | ||
| 11 | use base qw(XML::Template::Base); | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use XML::Template::Document; | ||
| 15 | |||
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | XML::Template::Cache - Document caching module for XML::Template. | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | use XML::Template::Cache; | ||
| 26 | |||
| 27 | my $cache = XML::Template::Cache->new (%config) | ||
| 28 | || die XML::Template::Cache->error; | ||
| 29 | my $document = XML::Template::Document->new (); | ||
| 30 | $cache->put ($docname, $document); | ||
| 31 | $document = $cache->get ($docname); | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This modules provides the basic document caching mechanism used by | ||
| 36 | XML::Template. Parsed (i.e., code has been generated) documents are | ||
| 37 | stored in a private array. When the array is full, putting a document in | ||
| 38 | the cache causes the oldest (access time) entry to be replaced. | ||
| 39 | |||
| 40 | In the initialization of L<XML::Template::Process>, a cache object is | ||
| 41 | placed at the beginning of the load and put chains of responsiblity. | ||
| 42 | Hence, every load and put operation on a document will result in the cache | ||
| 43 | being queried first. | ||
| 44 | |||
| 45 | =head1 CONSTRUCTOR | ||
| 46 | |||
| 47 | A constructor method C<new> is provided by L<XML::Template::Base>. A list | ||
| 48 | of named configuration parameters may be passed to the constructor. The | ||
| 49 | constructor returns a reference to a new cache object or undef if an error | ||
| 50 | occurred. If undef is returned, you can use the method C<error> to | ||
| 51 | retrieve the error. For instance: | ||
| 52 | |||
| 53 | my $cache = XML::Template::Cache->new (%config) | ||
| 54 | || die XML::Template::Cache->error; | ||
| 55 | |||
| 56 | |||
| 57 | The following named configuration parameters are supported by this module: | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item CacheSlots | ||
| 62 | |||
| 63 | The size of the cache array. This value will override the default value | ||
| 64 | C<$CACHE_SLOTS> in L<XML::Template::Config>. The default cache array size | ||
| 65 | if 5. | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =head1 PRIVATE METHODS | ||
| 70 | |||
| 71 | =head2 _init | ||
| 72 | |||
| 73 | This method is the internal initialization function called from | ||
| 74 | L<XML::Template::Base> when a new cache object is created. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub _init { | ||
| 79 | my $self = shift; | ||
| 80 | my %params = @_; | ||
| 81 | |||
| 82 | print "XML::Template::Cache::_init\n" if $self->{_debug}; | ||
| 83 | |||
| 84 | $self->{_cache} = (); | ||
| 85 | $self->{_cache_slots} = $params{CacheSlots} || XML::Template::Config->cache_slots | ||
| 86 | || return $self->error (XML::Template::Config->error); | ||
| 87 | |||
| 88 | $self->{_enabled} = 1; | ||
| 89 | |||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | =head1 PUBLIC METHODS | ||
| 96 | |||
| 97 | =head2 load | ||
| 98 | |||
| 99 | my $document = $cache->load ($docname); | ||
| 100 | |||
| 101 | The C<load> method, returns a document stored in the cache named by | ||
| 102 | C<$docname>. If no document is found, undef is returned. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub load { | ||
| 107 | my $self = shift; | ||
| 108 | my $name = shift; | ||
| 109 | |||
| 110 | # Find document in cache. | ||
| 111 | foreach my $entry (@{$self->{_cache}}) { | ||
| 112 | if ($entry->{name} eq $name) { | ||
| 113 | print "XML::Template::Cache::load : $name loaded from cache.\n" if $self->{_debug}; | ||
| 114 | return $entry->{document}; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | return undef; | ||
| 119 | } | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =head2 put | ||
| 124 | |||
| 125 | my $document = XML::Template::Document->new (Code => $code); | ||
| 126 | $cache->put ($docname, $document); | ||
| 127 | |||
| 128 | The C<put> method stores a document in the cache. If the cache is full, | ||
| 129 | the oldest accessed document is replaced. The first parameter is the | ||
| 130 | name of the document. The second parameter is the document to store. | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | sub put { | ||
| 135 | my $self = shift; | ||
| 136 | my $name = shift; | ||
| 137 | my $document = shift; | ||
| 138 | |||
| 139 | my $entry; | ||
| 140 | if (defined $self->{_cache}) { | ||
| 141 | # Search for entry. | ||
| 142 | foreach my $tentry (@{$self->{_cache}}) { | ||
| 143 | if ($tentry->{name} eq $name) { | ||
| 144 | $entry = $tentry; | ||
| 145 | last; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | # Entry exists - update time. | ||
| 150 | if (defined $entry) { | ||
| 151 | print "XML::Template::Cache::put : $name already cached - updating time.\n" if $self->{_debug}; | ||
| 152 | $entry->{time} = time; | ||
| 153 | |||
| 154 | # Entry does not exist - add to cache. | ||
| 155 | } else { | ||
| 156 | # Cache full - remove oldest entry. | ||
| 157 | if (scalar (@{$self->{_cache}}) == $self->{_cache_slots}) { | ||
| 158 | my $oldest_pos = 0; | ||
| 159 | my $i = 0; | ||
| 160 | for (my $i = 1; $i < scalar (@{$self->{_cache}}); $i++) { | ||
| 161 | if ($self->{_cache}->[$i]->{time} < | ||
| 162 | $self->{_cache}->[$oldest_pos]->{time}) { | ||
| 163 | $oldest_pos = $i; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | print "XML::Template::Cache::put : Removing oldest cache entry ($oldest_pos, $self->{_cache}->[$oldest_pos]->{name}).\n" if $self->{_debug}; | ||
| 167 | splice (@{$self->{_cache}}, $oldest_pos, 1); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | if (! defined $entry) { | ||
| 173 | # Add document to cache. | ||
| 174 | print "XML::Template::Cache::put : Caching $name.\n" if $self->{_debug}; | ||
| 175 | unshift (@{$self->{_cache}}, {name => $name, | ||
| 176 | time => time, | ||
| 177 | document => $document}); | ||
| 178 | } | ||
| 179 | |||
| 180 | return 1; | ||
| 181 | } | ||
| 182 | |||
| 183 | =pod | ||
| 184 | |||
| 185 | =head1 AUTHOR | ||
| 186 | |||
| 187 | Jonathan Waxman | ||
| 188 | <jowaxman@bbl.med.upenn.edu> | ||
| 189 | |||
| 190 | =head1 COPYRIGHT | ||
| 191 | |||
| 192 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 193 | All rights reserved. | ||
| 194 | |||
| 195 | This program is free software; you can redistribute it and/or | ||
| 196 | modify it under the same terms as Perl itself. | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | |||
| 201 | 1; |