1HTTP::Cache::TransparenUts(e3r)Contributed Perl DocumentHaTtTiPo:n:Cache::Transparent(3)
2
3
4
6 HTTP::Cache::Transparent - Cache the result of http get-requests
7 persistently.
8
10 use LWP::Simple;
11 use HTTP::Cache::Transparent;
12
13 HTTP::Cache::Transparent::init( {
14 BasePath => '/tmp/cache',
15 } );
16
17 my $data = get( 'http://www.sn.no' );
18
20 An implementation of http get that keeps a local cache of fetched pages
21 to avoid fetching the same data from the server if it hasn't been
22 updated. The cache is stored on disk and is thus persistent between
23 invocations.
24
25 Uses the http-headers If-Modified-Since and ETag to let the server
26 decide if the version in the cache is up-to-date or not.
27
28 The cache is implemented by modifying the LWP::UserAgent class to
29 seamlessly cache the result of all requests that can be cached.
30
32 HTTP::Cache::Transparent provides an init-method that sets the
33 parameters for the cache and overloads a method in LWP::UserAgent to
34 activate the cache.After init has been called, the normal LWP-methods
35 (LWP::Simple as well as the more full-fledged LWP::Request methods)
36 should be used as usual.
37
38 init
39 Initialize the HTTP cache. Takes a single parameter which is a
40 hashref containing named arguments to the object.
41
42 HTTP::Cache::Transparent::init( {
43
44 # Directory to store the cache in.
45 BasePath => "/tmp/cache",
46
47 # How many hours should items be kept in the cache
48 # after they were last requested?
49 # Default is 8*24.
50 MaxAge => 8*24,
51
52 # Print progress-messages to STDERR.
53 # Default is 0.
54 Verbose => 1,
55
56 # If a request is made for a url that has been requested
57 # from the server less than NoUpdate seconds ago, the
58 # response will be generated from the cache without
59 # contacting the server.
60 # Default is 0.
61 NoUpdate => 15*60,
62
63 # When a url has been downloaded and the response indicates that
64 # has been modified compared to the content in the cache,
65 # the ApproveContent callback is called with the HTTP::Response.
66 # The callback shall return true if the response shall be used and
67 # stored in the cache or false if the response shall be discarded
68 # and the response in the cache used instead.
69 # This mechanism can be used to work around servers that return errors
70 # intermittently. The default is to accept all responses.
71 ApproveContent => sub { return $_[0]->is_success },
72 } );
73
74 The directory where the cache is stored must be writable. It must
75 also only contain files created by HTTP::Cache::Transparent.
76
77 Initializing from use-line
78 An alternative way of initializing HTTP::Cache::Transparent is to
79 supply parameters in the use-line. This allows you to write
80
81 use HTTP::Cache::Transparent ( BasePath => '/tmp/cache' );
82
83 which is exactly equivalent to
84
85 use HTTP::Cache::Transparent;
86 HTTP::Cache::Transparent::init( BasePath => '/tmp/cache' );
87
88 The advantage to using this method is that you can do
89
90 perl -MHTTP::Cache::Transparent=BasePath,/tmp/cache myscript.pl
91
92 or even set the environment variable PERL5OPT
93
94 PERL5OPT=-MHTTP::Cache::Transparent=BasePath,/tmp/cache
95 myscript.pl
96
97 and have all the http-requests performed by myscript.pl go through
98 the cache without changing myscript.pl
99
101 The HTTP::Cache::Transparent inserts two special headers in the
102 HTTP::Response object. These can be accessed via the
103 HTTP::Response::header()-method.
104
105 X-Cached
106 This header is inserted and set to 1 if the response is delivered
107 from the cache instead of from the server.
108
109 X-Content-Unchanged
110 This header is inserted and set to 1 if the content returned is the
111 same as the content returned the last time this url was fetched.
112 This header is always inserted and set to 1 when the response is
113 delivered from the cache.
114
116 This module has a number of limitations that you should be aware of
117 before using it.
118
119 - There is no upper limit to how much diskspace the cache requires.
120 The only limiting mechanism is that data for urls that haven't been
121 requested in the last MaxAge hours will be removed from the cache
122 the next time the program exits.
123
124 - Currently, only get-requests that store the result in memory (i.e.
125 do not use the option to have the result stored directly in a file
126 or delivered via a callback) is cached. I intend to remove this
127 limitation in a future version.
128
129 - The support for Ranges is a bit primitive. It creates a new object
130 in the cache for each unique combination of url and range. This
131 will work ok as long as you always request the same range(s) for a
132 url.
133
134 - The cache doesn't properly check and store all headers in the HTTP
135 request and response. Therefore, if you request the same url
136 repeatedly with different sets of headers (cookies, accept-encoding
137 etc), and these headers affect the response from the server, the
138 cache may return the wrong response.
139
141 The cache is stored on disk as one file per cached object. The filename
142 is equal to the md5sum of the url and the Range-header if it exists.
143 The file contains a set of key/value-pairs with metadata (one entry per
144 line) followed by a blank line and then the actual data returned by the
145 server.
146
147 The last modified date of the cache file is set to the time when the
148 cache object was last requested by a user.
149
151 Mattias Holmlund, <$firstname -at- $lastname -dot- se>
152 <http://www.holmlund.se/mattias/>
153
155 A git repository containing the source for this module can be found via
156 http://git.holmlund.se/
157
159 Copyright (C) 2004-2007 by Mattias Holmlund
160
161 This library is free software; you can redistribute it and/or modify it
162 under the same terms as Perl itself, either Perl version 5.8.4 or, at
163 your option, any later version of Perl 5 you may have available.
164
165
166
167perl v5.12.0 2007-12-12 HTTP::Cache::Transparent(3)