Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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?
[shmem]: trying to build a RPM for some perl with a bunch of modules. Trying perlbrew, trying alien.
[stevieb]: finally got my 12v solar setup for my Pi/Arduino, with a 12v 7.2Ah battery. Testing how long the Pi will run on the fully charged battery before I hook it up to the solar regulator. I expect between 8 and 18 hours, depending on load.
[shmem]: ...writing a Makefile for that. Edit, test, lather, rinse, repeat.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2017-03-25 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (313 votes). Check out past polls.