Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Object Caching in Modules: Examples?

by ian (Beadle)
on Dec 07, 2007 at 20:04 UTC ( #655732=perlquestion: print w/replies, xml ) Need Help??
ian has asked for the wisdom of the Perl Monks concerning the following question:

While reading HOP, I got interested in memoizing or otherwise adding caching to object methods; accessing data from some sources to create commonly requested objects makes my app slow.

Has anyone had any experience with this or can otherwise point me in the direction of example modules?

I should mention that I've been over to the tutorials and have found:

  1. Cloning Pieces of an Object
  2. static object caches
-- Ian Tegebo

Replies are listed 'Best First'.
Re: Object Caching in Modules: Examples?
by Fletch (Chancellor) on Dec 07, 2007 at 20:14 UTC

    Memoize or the other techniques shown in HOP should work, the thing to remember is that you really can only cache "value objects" which are immutable. And if the instances aren't intrinsically immutable you want to be very careful to use whatever clone method or copy constructor is provided before you do make changes to them.

    As an example we use a wrapper class at $work around DateTime instances, and I often wind up writing a caching sub to call the wrapper class' convenience constructor for "mm/dd/yyyy" dates:

    { my %_mdy_cache; sub _from_mdy { my $mdy = shift; return $_mdy_cache{ $mdy } ||= Our::DateTime->from_mdy( $mdy, "/" +); } }

    But you've then got to be very careful to call $dt_instance->clone() before changing the instances because otherwise you wind up altering the cached instances and the next person to ask for "m/d/y" gets "m/d+2/y" because you didn't before you called $foo->add( days => 2 ).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://655732]
Approved by TStanley
help
Chatterbox?
[r@j]: I am new to perl script, seeking your advice for the following requirement in perl script - Need to retrieve the status of running process with the process name without using ps -ef command.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2017-10-24 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (286 votes). Check out past polls.

    Notices?