Cache::Entry(3pm)
π Cache::Entry β¨
okay so bestie, welcome to Cache::Entry(3pm) β that's the User Contributed Perl Documentation one, and it's literally the interface for a cache entry π. Sit down, grab a strawberry, we're doing this.
πΈ NAME πΈ
Cache::Entry β the interface for a cache entry. that's it, that's the name, short and iconic π
π¦ SYNOPSIS π¦
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');
}
look at that flow. check if it exists, grab it if it does, otherwise go get the data and set it with a lil expiry. chef's kiss, no notes π
β¨ DESCRIPTION β¨
so. objects derived from Cache::Entry represent an entry in a Cache. the methods are all about acting on the data in that entry, and letting you set stuff like the expiry time. very main character, very "I hold your data and I know when it dies" π
important bestie!! you should NOT create instances ofCache::Entrydirectly. do not do it. instead, use theentry($key)method of aCacheinstance. that's the way. periodt.
π METHODS π
my $cache = $e->cache()β hands you back a reference to the cache object this entry came from. it's giving "who's your parent" π«Άmy $key = $e->key()β returns the cache key this entry is associated with. its little name tag π·οΈmy $bool = $e->exists()β returns a boolean value (1or0) telling you whether there's any data present in the cache for this entry. yes or no, no in-between β¨$e->set( $data, [ $expiry ] )β stores the data into the cache. the data must be a scalar (if you wanna store more complex data types, go look atfreezeandthawbelow, i got you). the expiry time can be passed as an optional 2nd argument, and it's in the same form as for'set_expiry($time)'.my $data = $e->get()β returns the data from the cache, orundefif the entry doesn't exist. if it's not there, it's just not there, don't take it personally πmy $size = $e->size()β returns the size of the entry data, orundefif the entry doesn't exist. measuring the fit π$e->remove()β clears the data for this entry from the cache. bye bye data, you were loved π§my $expiry = $e->expiry()β returns the expiry time of the entry, in seconds since the epoch. ticking clock energy β°$e->set_expiry( $time )β sets the expiry time in seconds since the epoch, or you can use a string like'10 minutes'. valid units ares,second,seconds,sec,m,minute,minutes,min,h,hour,hours,w,week,weeks,M,month,months,y,yearandyears. (yes bestie, lowercasemis minutes and uppercaseMis months β read it twice, i know π) you can also specify an absolute time, such as'16 Nov 94 22:28:20'or any other time thatDate::Parsecan understand. and finally, the strings'now'and'never'may also be used.'never'is such a mood ngl πmy $fh = $e->handle( [$mode, [$expiry] ] )β returns anIO::Handleby which data can be read, or written, to the cache. this is useful if you're caching a large amount of data β although it should be noted that only some cache implementations (such asCache::File) provide an efficient mechanism for implementing this. so like, check your implementation before you get obsessed π- the optional mode argument can be any of the perl mode strings as used for the
openfunction:'<','+<','>','+>','>>'and'+>>'. alternatively it can be the correspondingfopen(3)modes of'r','r+','w','w+','a'and'a+'. the default mode is'+<'(or'r+') indicating reading and writing. both directions, the full experience π«Ά - 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. so like, no expiry given = cache's own default takes the wheel π - 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. usingset()orremove(), 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.
- the optional mode argument can be any of the perl mode strings as used for the
bestie, listen. the locking thing is not a vibe suggestion, it's real. writers get an exclusive lock, readers get a shared one. don't callget()or grab another handle while you're holding a write handle. and if youset()orremove()while a handle is open β those handles stick around but they're now pointing at old data, and any changes you write to the data get discarded. ancient relic behaviour π
π STORING VALIDITY OBJECTS π
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. receipts for your receipts, iykyk π
$e->validity()
$e->set_validity( $data )
π§ STORING COMPLEX OBJECTS π§
the set and get methods only allow for working with simple scalar types, but if you wanna 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.
WARNING, NO CAP: you must be careful to ONLY use'thaw'on data that was stored via'freeze'. otherwise the stored data won't actually be inStorableformat and it will complain loudly. loudly, bestie. it will scream π
$e->freeze( $data, [ $expiry ] )β identical to'set', except that data may be any complex data type that can be serialized viaStorable. βοΈ$e->thaw()β identical to'get', except that it will return a complex data type that was set via'freeze'. defrosted and gorgeous π₯
π SEE ALSO π
Cache, Cache::File
go read them, they're the supporting cast and they ate β
π AUTHOR π
Chris Leishman <chris@leishman.org>
Based on work by DeWitt Clinton <dewitt@unto.net>
two icons, one module π«Ά
π COPYRIGHT π
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 $
and that's the whole page, bestie!! π this was documented by perl v5.34.0 and dated 2022-06-09 β so go forth, cache responsibly, and remember: thaw only what you freeze. slay π
β¨
π¦ where this page comes from
- debian bookworm: libcache-perl 2.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcache-perl 2.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcache-perl 2.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcache-perl 2.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcache-perl 2.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms