You are here: Re: Advice about fetching user information « PHP Programming Language « IT news, forums, messages
Re: Advice about fetching user information

Posted by Erwin Moller on 11/23/06 15:03

Sandman wrote:

> So, I have this content management system I've developed myself. The
> system has a solid community part where members can register and then
> participate in forums, write weblogs and a ton of other things.
>
> So, in instances where I list, for example, the latest weblogs. I list
> the headline of the weblog, the date and the name of the member who
> wrote it.
>
> Now, the name isn't just "Smith", but rather Smith's online status,
> his nick and his "label", and can look like this:
>
> <image> Smith M:34 (Newbie)
>
> The image is either a green or red dot depicting his online status.
> When I want to display this, I use this function:
>
> print member_name(12);
>
> With 12 being the id number of Smith, and the only information about
> the writer of the weblog (i.e. I don't save his nick or label in the
> weblog post, and certainly not his online status).
>
> So, when using member_name() I look up the id 12 in a prefetched array
> of members, displaying the correct information. In a compact format:
>
> ## index.php
> require_once("member.php");
> print member_name(12);
>
> ## member.php
> $q=mysql_query("select id, name, label from members");
> while ($r=mysql_fetch_assoc($q)){
> $GLOBALS["members"][$r["id"]] = $r;
> }
>
> function member_name($id){
> $m = $GLOBALS["members"][$id];
> $online = in_array($id, $GLOBALS["surfers"]) ? "on" : "off";
> return icon("online/$online") . " $m[name] $m[label]";
> }
>
> ##
>
> So, whenever requiring member.php, all members are prefetched and put
> into $GLOBALS["members"], which has worked just fine. Then I have
> infromation about any given member at my fingertips.
>
> But now I have some sites which are beginning to get a pretty large
> number of members, so prefetching them all becomes timeconsuming
> (sometimes up to one second, which is unacceptably slow).
>
> I can't fetch information about each single individual at the point of
> showin his or hers information since there can be long lists of for
> example weblogs and there might be 40-50 member names to be shown,
> which would result in 40-50 seperate mysql requests, which would flood
> the MySSQL server, I'm afraid.
>
>
> So, my question is this; what other way can I do this in? Is there a
> faster way to read thousands or even tens of thousands of information
> points and put them in an array quickly?
>
> Or is there a better way to read information about single members at
> runtime?
>
> Aggregation is most certainly an option. Can I write a tab separated
> text file and read that one in member.txt - would it be faster? Should
> I write one text file per user and read that file when needed?
>
> Anyone got any experience in doing this? How did you solve it?
>
> Thanks in advance. :)
>
>
>
>


Hi,


First: Prefetching all members into memory for each page where you need one
or two members is absolutely a no-go. You must change that of course.
Just fetch the information you need WHEN you need it.

If I understand correctly your main problem lies in the fact you must fetch
the online/offline information for each possible user.
Correct?

This is how I solved this problem when I was in a similar situation:
(a rough quick overview to show the idea)

1) Create a table like
CREATE TABLE tblOnlineUsers(
userid integer REFERENCES tbluser.userid,
lastActive timestamp
)

2) Whenever a logged-in user does something, update the timestamp in this
table for that userid.

3) When you are on a page that needs to know the status of online users,
just load it into a array with a query like:
SELECT userid FROM tblOnlineUsers WHERE (
lastActive > XXX
)

Where XXX is (now - your sessiontimeout).

Now you need some logic to delete the userids for stale users.
(Users that didn't log out, but just closed their browser or walked away to
get a nice good lunch of three hours)
eg
When a random number between 0 and 1 is smaller than 0.1 you delete all
records older than the sessiontimeout.

You also need to make sure the userid in that table is unique.

But this solution has the advantage that:
- You don't load all users into memory for each scriptinvocation
- Your tblOnlineUsers can be searched very quickly.

Hope that helps.

Regards,
Erwin Moller

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация