Cache::Entry(3pm) | User Contributed Perl Documentation | Cache::Entry(3pm) |
Cache::Entry - interface for a cache entry
my Cache::Entry $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.
The expiry time may be provided as an optional 2nd argument and is in the same form as for 'set_expiry($time)'.
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 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 handle 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.
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, however, that you must be careful to ONLY use 'thaw' on data that was stored via 'freeze'. Otherwise the stored data wont actually be in Storable format and it will complain loudly.
Cache, Cache::File
Chris Leishman <chris@leishman.org> Based on work by DeWitt Clinton <dewitt@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 $
2022-06-09 | perl v5.34.0 |