Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://655732]
Approved by TStanley
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-05-22 03:29 GMT
Find Nodes?
    Voting Booth?