1Cache::Entry(3) User Contributed Perl Documentation Cache::Entry(3)
2
3
4
6 Cache::Entry - interface for a cache entry
7
9 my Cache::Entry $entry = $cache->entry( $key )
10 my $data;
11 if ($entry->exists()) {
12 $data = $entry->get();
13 }
14 else {
15 $data = get_some_data($key);
16 $entry->set($data, '10 minutes');
17 }
18
20 Objects derived from Cache::Entry represent an entry in a Cache.
21 Methods are provided that act upon the data in the entry, and allow you
22 to set things like the expiry time.
23
24 Users should not create instances of Cache::Entry directly, but instead
25 use the entry($key) method of a Cache instance.
26
28 my $cache = $e->cache()
29 Returns a reference to the cache object this entry is from.
30
31 my $key = $e->key()
32 Returns the cache key this entry is associated with.
33
34 my $bool = $e->exists()
35 Returns a boolean value (1 or 0) to indicate whether there is any
36 data present in the cache for this entry.
37
38 $e->set( $data, [ $expiry ] )
39 Stores the data into the cache. The data must be a scalar (if you
40 want to store more complex data types, see freeze and thaw below).
41
42 The expiry time may be provided as an optional 2nd argument and is
43 in the same form as for 'set_expiry($time)'.
44
45 my $data = $e->get()
46 Returns the data from the cache, or undef if the entry doesn't
47 exist.
48
49 my $size = $e->size()
50 Returns the size of the entry data, or undef if the entry doesn't
51 exist.
52
53 $e->remove()
54 Clear the data for this entry from the cache.
55
56 my $expiry = $e->expiry()
57 Returns the expiry time of the entry, in seconds since the epoch.
58
59 $e->set_expiry( $time )
60 Set the expiry time in seconds since the epoch, or alternatively
61 using a string like '10 minutes'. Valid units are s, second,
62 seconds, sec, m, minute, minutes, min, h, hour, hours, w, week,
63 weeks, M, month, months, y, year and years. You can also specify
64 an absolute time, such as '16 Nov 94 22:28:20' or any other time
65 that Date::Parse can understand. Finally, the strings 'now' and
66 'never' may also be used.
67
68 my $fh = $e->handle( [$mode, [$expiry] ] )
69 Returns an IO::Handle by which data can be read, or written, to the
70 cache. This is useful if you are caching a large amount of data -
71 although it should be noted that only some cache implementations
72 (such as Cache::File) provide an efficient mechanism for
73 implementing this.
74
75 The optional mode argument can be any of the perl mode strings as
76 used for the open function '<', '+<', '>', '+>', '>>' and '+>>'.
77 Alternatively it can be the corresponding fopen(3) modes of 'r',
78 'r+', 'w', 'w+', 'a' and 'a+'. The default mode is '+<' (or 'r+')
79 indicating reading and writing.
80
81 The second argument is used to set the expiry time for the entry if
82 it doesn't exist already and the handle is opened for writing. It
83 is also used to reset the expiry time if the entry is truncated by
84 opening in the '>' or '+>' modes. If the expiry is not provided in
85 these situations then the default expiry time for the cache is
86 applied.
87
88 Cache implementations will typically provide locking around cache
89 entries, so that writers will have have an exclusive lock and
90 readers a shared one. Thus the method get() (or obtaining another
91 handle) should be avoided whilst a write handle is held. Using
92 set() or remove(), however, should be supported. These clear the
93 current entry and whilst they do not invalidate open handles, those
94 handle will from then on refer to old data and any changes to the
95 data will be discarded.
96
98 There are two additional set & get methods that can be used to store a
99 validity object that is associated with the data in question.
100 Typically this is useful in conjunction with a validate_callback, and
101 may be used to store a timestamp or similar to validate against. The
102 validity data stored may be any complex data that can be serialized via
103 Storable.
104
105 $e->validity()
106 $e->set_validity( $data )
107
109 The set and get methods only allow for working with simple scalar
110 types, but if you want to store more complex types they need to be
111 serialized first. To assist with this, the freeze and thaw methods are
112 provided. They are simple wrappers to get & set that use Storable to
113 do the serialization and de-serialization of the data.
114
115 Note, however, that you must be careful to ONLY use 'thaw' on data that
116 was stored via 'freeze'. Otherwise the stored data wont actually be in
117 Storable format and it will complain loudly.
118
119 $e->freeze( $data, [ $expiry ] )
120 Identical to 'set', except that data may be any complex data type
121 that can be serialized via Storable.
122
123 $e->thaw()
124 Identical to 'get', except that it will return a complex data type
125 that was set via 'freeze'.
126
128 Cache, Cache::File
129
131 Chris Leishman <chris@leishman.org>
132 Based on work by DeWitt Clinton <dewitt@unto.net>
133
135 Copyright (C) 2003-2006 Chris Leishman. All Rights Reserved.
136
137 This module is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
138 KIND, either expressed or implied. This program is free software; you
139 can redistribute or modify it under the same terms as Perl itself.
140
141 $Id: Entry.pm,v 1.8 2006/01/31 15:23:58 caleishm Exp $
142
143
144
145perl v5.30.1 2020-01-29 Cache::Entry(3)