1App::Cache(3)         User Contributed Perl Documentation        App::Cache(3)
2
3
4

NAME

6       App::Cache - Easy application-level caching
7

SYNOPSIS

9         # in your class:
10         my $cache = App::Cache->new({ ttl => 60*60 });
11         $cache->delete('test');
12         my $data = $cache->get('test');
13         my $code = $cache->get_code("code", sub { $self->calculate() });
14         my $html = $cache->get_url("http://www.google.com/");
15         $cache->set('test', 'one');
16         $cache->set('test', { foo => 'bar' });
17         my $scratch = $cache->scratch;
18         $cache->clear;
19

DESCRIPTION

21       The App::Cache module lets an application cache data locally. There are
22       a few times an application would need to cache data: when it is
23       retrieving information from the network or when it has to complete a
24       large calculation.
25
26       For example, the Parse::BACKPAN::Packages module downloads a file off
27       the net and parses it, creating a data structure. Only then can it
28       actually provide any useful information for the programmer.
29       Parse::BACKPAN::Packages uses App::Cache to cache both the file
30       download and data structures, providing much faster use when the data
31       is cached.
32
33       This module stores data in the home directory of the user, in a dot
34       directory. For example, the Parse::BACKPAN::Packages cache is actually
35       stored underneath "~/.parse_backpan_packages/cache/". This is so that
36       permisssions are not a problem - it is a per-user, per-application
37       cache.
38

METHODS

40   new
41       The constructor creates an App::Cache object. It takes three optional
42       parameters:
43
44       ·   ttl contains the number of seconds in which a cache entry expires.
45           The default is 30 minutes.
46
47             my $cache = App::Cache->new({ ttl => 30*60 });
48
49       ·   application sets the application name. If you are calling new()
50           from a class, the application is automagically set to the calling
51           class, so you should rarely need to pass it in:
52
53             my $cache = App::Cache->new({ application => 'Your::Module' });
54
55       ·   directory sets the directory to be used for the cache. Normally
56           this is just set for you and will be based on the application name
57           and be created in the users home directory. Sometimes for testing,
58           it can be useful to set this.
59
60             my $cache = App::Cache->new({ directory => '/tmp/your/cache/dir' });
61
62       ·   enabled can be set to 0 for testing, in which case you will always
63           get cache misses:
64
65             my $cache = App::Cache->new({ enabled => 0 });
66
67   clear
68       Clears the cache:
69
70         $cache->clear;
71
72   delete
73       Deletes an entry in the cache:
74
75         $cache->delete('test');
76
77   get
78       Gets an entry from the cache. Returns undef if the entry does not exist
79       or if it has expired:
80
81         my $data = $cache->get('test');
82
83   get_code
84       This is a convenience method. Gets an entry from the cache, but if the
85       entry does not exist, set the entry to the value of the code reference
86       passed:
87
88         my $code = $cache->get_code("code", sub { $self->calculate() });
89
90   get_url
91       This is a convenience method. Gets the content of a URL from the cache,
92       but if the entry does not exist, set the entry to the content of the
93       URL passed:
94
95         my $html = $cache->get_url("http://www.google.com/");
96
97   scratch
98       Returns a directory in the cache that the application may use for
99       scratch files:
100
101         my $scratch = $cache->scratch;
102
103   set
104       Set an entry in the cache. Note that an entry value may be an arbitrary
105       Perl data structure:
106
107         $cache->set('test', 'one');
108         $cache->set('test', { foo => 'bar' });
109
110   directory
111       Returns the full path to the cache directory. Primarily useful for when
112       you are writing tests that use App::Cache and want to clean up after
113       yourself. If you are doing that you may want to explicitly set the
114       'application' constructor parameter to avoid later cleaning up a cache
115       dir that was already in use.
116
117         my $dir = $cache->directory;
118

AUTHOR

120       Leon Brocard <acme@astray.com>
121
123       Copyright (C) 2005-7, Leon Brocard
124

LICENSE

126       This module is free software; you can redistribute it or modify it
127       under the same terms as Perl itself.
128
129
130
131perl v5.30.0                      2019-07-26                     App::Cache(3)
Impressum