Cache::Entry â interface for a cache entry
| Use Case | Command | Description |
|---|---|---|
| Get entry from cache | $entry = $cache->entry($key) | Create or retrieve a cache entry object |
| Check existence | $entry->exists() | Returns true if data is cached for this entry |
| Read data | $entry->get() | Retrieve cached data (or undef if missing) |
| Write data | $entry->set($data, $expiry) | Store data with optional expiry time |
| Remove entry | $entry->remove() | Clear cached data for this entry |
| Get expiry time | $entry->expiry() | Returns expiry time in seconds since epoch |
| Set expiry time | $entry->set_expiry($time) | Set expiry as absolute time or relative string |
| Open filehandle | $entry->handle($mode, $expiry) | Get an IO::Handle for reading/writing large data |
| Store complex objects | $entry->freeze($data, $expiry) | Serialize any complex data via Storable |
| Retrieve complex objects | $entry->thaw() | Deserialize data previously stored with freeze |
my $entry = $cache->entry( $key )
my $data;
if ($entry->exists()) {
$data = $entry->get();
}
else {
$data = get_some_data($key);
$entry->set($data, '10 minutes');
}
Objects derived from Cache::Entry represent an entry in a Cache. Methods are provided that act upon the data in the entry, and allow you to set things like the expiry time.
Users should not create instances of Cache::Entry directly, but instead use the entry($key) method of a Cache instance.
my $cache = $e->cache() â Returns a reference to the cache object this entry is from.my $key = $e->key() â Returns the cache key this entry is associated with.my $bool = $e->exists() â Returns a boolean value (1 or 0) to indicate whether there is any data present in the cache for this entry.$e->set( $data, [ $expiry ] ) â Stores the data into the cache. The data must be a scalar (if you want to store more complex data types, see freeze and thaw below). The expiry time may be provided as an optional 2nd argument and is in the same form as for set_expiry($time).my $data = $e->get() â Returns the data from the cache, or undef if the entry doesn't exist.my $size = $e->size() â Returns the size of the entry data, or undef if the entry doesn't exist.$e->remove() â Clear the data for this entry from the cache.my $expiry = $e->expiry() â Returns the expiry time of the entry, in seconds since the epoch.$e->set_expiry( $time ) â Set the expiry time in seconds since the epoch, or alternatively using a string like '10 minutes'. Valid units are s, second, seconds, sec, m, minute, minutes, min, h, hour, hours, w, week, weeks, M, month, months, y, year and years. You can also specify an absolute time, such as '16 Nov 94 22:28:20' or any other time that Date::Parse can understand. Finally, the strings 'now' and 'never' may also be used.my $fh = $e->handle( [$mode, [$expiry] ] ) â Returns an IO::Handle by which data can be read, or written, to the cache. This is useful if you are caching a large amount of data â although it should be noted that only some cache implementations (such as Cache::File) provide an efficient mechanism for implementing this. The optional mode argument can be any of the perl mode strings as used for the open function '<', '+<', '>', '+>', '>>' and '+>>'. Alternatively it can be the corresponding fopen(3) modes of 'r', 'r+', 'w', 'w+', 'a' and 'a+'. The default mode is '+<' (or 'r+') indicating reading and writing. The second argument is used to set the expiry time for the entry if it doesn't exist already and the handle is opened for writing. It is also used to reset the expiry time if the entry is truncated by opening in the '>' or '+>' modes. If the expiry is not provided in these situations then the default expiry time for the cache is applied. Cache implementations will typically provide locking around cache entries, so that writers will have an exclusive lock and readers a shared one. Thus the method get() (or obtaining another handle) should be avoided whilst a write handle is held. Using set() or remove(), however, should be supported. These clear the current entry and whilst they do not invalidate open handles, those handles will from then on refer to old data and any changes to the data will be discarded.There are two additional set & get methods that can be used to store a validity object that is associated with the data in question. Typically this is useful in conjunction with a validate_callback, and may be used to store a timestamp or similar to validate against. The validity data stored may be any complex data that can be serialized via Storable.
$e->validity()$e->set_validity( $data )The set and get methods only allow for working with simple scalar types, but if you want to store more complex types they need to be serialized first. To assist with this, the freeze and thaw methods are provided. They are simple wrappers to get & set that use Storable to do the serialization and de-serialization of the data.
Note: You must be careful to ONLY use thaw on data that was stored via freeze. Otherwise the stored data won't actually be in Storable format and it will complain loudly.
$e->freeze( $data, [ $expiry ] ) â Identical to set, except that data may be any complex data type that can be serialized via Storable.$e->thaw() â Identical to get, except that it will return a complex data type that was set via freeze.Chris Leishman <chris AT leishman.org>
Based on work by DeWitt Clinton <dewitt AT unto.net>
Copyright (C) 2003-2006 Chris Leishman. All Rights Reserved.
This module is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or implied. This program is free software; you can redistribute or modify it under the same terms as Perl itself.
$Id: Entry.pm,v 1.8 2006/01/31 15:23:58 caleishm Exp $
Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-14 21:50 @2600:1f28:365:80b0:4d23:66fa:c2bb:7bae
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)