Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Caching a hash?

by guice (Scribe)
on Mar 09, 2006 at 18:20 UTC ( #535459=perlquestion: print w/replies, xml ) Need Help??

guice has asked for the wisdom of the Perl Monks concerning the following question:

What are some techniques for caching a hash? I have a rather large Perl script that tends to get a bit heavy in CPU and memory. To assist in alleviating that, I want to start caching some larger hashes (so they don't remain in memory when not needed).

In PHP, I can just serialize an array and write it to a file in /tmp for a "quick cache" and then load it up later on in the script for the specific functions/methods I need it in. But I can't think of any method in Perl that would resemble that. I'm looking for methods using more of the built in Perl methods and/or default Perl modules.

Do you all do hash caching and if so how do you do it?

Before somebody suggests a third party module: it's important to note I have limited access to Perl modules. Code is on an internal system with no Internet access and the requirements to get *anything* added to even just Perl modules is insane. Not to mention we have no local environment to compile code.

Thanks much!

-- philip
We put the 'K' in kwality!

Replies are listed 'Best First'.
Re: Caching a hash?
by duff (Parson) on Mar 09, 2006 at 18:23 UTC
Re: Caching a hash?
by moot (Chaplain) on Mar 09, 2006 at 18:24 UTC
    Have you looked at Data::Dumper ? Not sure if it'll be on your system or not, but it will pretty much do what you want.
      Storable is faster and has been in the core Perl distribution for years.

        Thanks, to both of you. Storable looks like a nice and quick way to unload and reload a hash quickly w/out all the fancy programming required for ties. I got a quick, read 10 minute, method written out that takes into the account of storing and retrieving (very easy). Now I just gotta test it out (!!).

        -- philip
        We put the 'K' in kwality!

Re: Caching a hash?
by pboin (Deacon) on Mar 09, 2006 at 19:36 UTC

    You may also want to read up on Memoize. I saw it presented lately, and was pretty impressed w/ what it does. It might fit into your problem in a way you haven't thought of yet. It's trivially easy to implement, too. Here's the summary:

    'Memoizing' a function makes it faster by trading space for time. It does this by caching the return values of the function in a table. If you call the function again with the same arguments, memoize jumps in and gives you the value out of the table, instead of letting the function compute the value all over again.

    Oh, and as to the module problem, if you have write access for the source that you've written, there's no reason why you can't lay down code that comes from CPAN too. After all, it's (usually) just Perl code, there's nothing magic about it.

      I'll check that out. I've gotten in the habit of using variable caching for methods I might repeatably call (ie, db method to return the db object) and use did basic isset() checks; setting the value if false and returning the variable.

      That can be useful for methods were I have a rather large return array and want to free up some memory instead of keeping the values stored in a local variable (pretty much what I'm needing and more). Thanks!

      And the module part, generally this is true although some stronger modules (especially ones that start playing around with memory and system level stuff) requires compiled code. So I just wanted to make sure I cleared that restriction before I get a slew of mem_cache type modules suggested

      -- philip
      We put the 'K' in kwality!

Re: Caching a hash?
by davidrw (Prior) on Mar 09, 2006 at 19:54 UTC
    Cache::FileCache basically wraps up the "Dumper to a file in /tmp" process and is easy to work with..

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://535459]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-09-28 10:19 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (124 votes). Check out past polls.