Tomasi, Chuck [[EMAIL PROTECTED]] wrote:
> 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
> sub GetUser
> {
>       my ($id, $user)=@_;     # record number and hash reference to
                 ^^^^^

As Stephen said, this is lexically scoped - when you assign a new
value to $user from $UserCache[$id], the new $user's scope ends
at the end of this subroutine.

I think you want to return the new $user to the caller, so that
the caller will be aware of the new $user, like this
below:


> populate
> 
>       if (defined($UserCache[$id])) {
> 
>         $user = $UserCache[$id];                                            
>         ###return(1);                                                          
          return $UserCache[$id];

>       }
> 
> 
>       # Code here to read info from the database and put it in $user as
> necessary
> 
>       # Store the info from the DB for later
>       #$UserCache[$id] = $user;
> 
>       #return 1;

        return $user;

HTH.

-- 
Hardy Merrill
Mission Critical Linux, Inc.
http://www.missioncriticallinux.com

> 
> }
> 
> I've verified $user gets populated properly when data comes from the
> database (fields are populated individually) and I've also verified that
> data is being stored in @UserCache properly on subsequent hits, but it isn't
> getting from $UserCache[$id] in to $user as expected and I don't know why.
> Do I need to fill up $user fields individually when retrieving from the
> cache?  Has anyone done anything like this before?
> 
> Thanks
> 
> --Chuck

Reply via email to