That's just about what I just came up with for a working model except I like
your idea better.  I was just about to inform my partner that I had to
change the usage of GetUser throughout the various programs (ugh!).

It's still going to be some considerable work to convert what I've already
written (some 10,000 lines of code), but it isn't insurmountable and the
payoff will be worth it.

Thanks.

--Chuck

> -----Original Message-----
> From: Hardy Merrill [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, January 15, 2002 4:17 PM
> To: Tomasi, Chuck
> Cc: '[EMAIL PROTECTED]'
> Subject: Re: Maintaining a Cache of Hash References
> 
> 
> You could do something like this - instead of using hash %uref,
> you could create a reference to an anonymous hash, and then use
> that reference to 1) refer to hash elements, *AND* 2) to pass
> to inc::db::GetUser - something like this:
> 
> caller
> ------
> 
>   $uref = {}; ### produces a *reference* to an anonymous array
> 
>   $uref = inc::db::GetUser($val1, $uref);
> 
>   print "User's Name is $uref->{FIRST_NAME}";
> 
> 
> and GetUser might look like this:
> ---------------------------------
> 
>  @UserCache;     # Place to store data already seen
>  sub GetUser
>  {
>     my ($id, $user_hashref)=@_; # record number and hash 
> reference to populate
> 
>     if (defined($UserCache[$id])) {
> 
>          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_hashref;
> 
>     return $user_hashref;
>  }
> 
> 
> You get the idea - since the $user_hashref is lexically scoped
> with "my" in subroutine GetUser, you need to be sure the reference
> to the hash makes it out of the subroutine and back to the calling
> code.
> 
> HTH.
> 
> -- 
> Hardy Merrill
> Mission Critical Linux, Inc.
> http://www.missioncriticallinux.com
> 
> 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
> > populate
> > 
> >     if (defined($UserCache[$id])) {
> > 
> >         $user = $UserCache[$id];                            
>                 
> >         return(1);                                          
>                 
> >     }
> > 
> > 
> >     # 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;
> > }
> > 
> > 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