On Jan 15, Tomasi, Chuck said:

>I'm trying to maintain a cache of hashes to reduce database hits.  What I
>want is to determine if I've retrieved the data from the DB before, if so,
>just pass back the copy of information used last time, otherwise read it
>from the DB and make a note of it.  It would seem I'm not copying the
>information in to the $user arg properly.  Something like this:
>
>@UserCache;    # Place to store data already seen

Arrays are not the best structures for caches, because what if all the
accessing I do is for users with IDs like 1043 and 4893?  You've got a
sparsely populated array.

I'd suggest a hash instead.

>sub GetUser
>{
>       my ($id, $user)=@_;     # record number and hash reference to
>populate
>
>       if (defined($UserCache[$id])) {
>        $user = $UserCache[$id];                                            
>        return(1);                                                          
>       }
>
>       # Store the info from the DB for later
>       $UserCache[$id] = $user;
>
>       return 1;
>}

To change the value PASSED to the function, you must modify the
corresponding element in @_:

  $_[1] = $UserCache[$id];

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to