简体   繁体   中英

PHP/MySQL: Best way to fetch new entries?

We've got some kind of groups at our site where group members could put messages. I want to notice the members of each group when there's new entries.

What's the best way to do this?

If you want to notify them immediately, you could just send them an email/whatever immediately as you store the message.

Otherwise you could store the highest 'read' messageid against each user, then you can fairly easily fetch any subseqent messages.

If there possible will be a lot of messages per group than it worth to have group-level field like LastNotificationTime and then schedule task to look for messages with created time later that current LastNotificationTime value (then send report about them to users) and update LastNotificationTime to current time.

Even better is to have user-level LastActivityTimeInGroup field and compare message creation date with it (maybe adding some minutes to it for smoother experience) so only the inactive at that time users will receive any messages and if any not more than 1 per task interval time.

I currently use a key store for this purpose (memcachedb). what you could do is to add a key such as 'messageread_{message.id}_{user.id}' set to whatever value you choose (i just use int 1), then use a memcached client to do a getmulti for all the messages the user has (as an array of keys posted above). any key that is returned is read :)

I use this method with a 60 second cache of this query itself to give users an unread message count with great results.

http://memcachedb.org/

i originally used a highest id type check and soon after switched to a multi-level comment system, thus making highest id moot in that context (as you could end up seeing a comment and miss one that is a reply). so far this method has been very fast.

to do something like this you would need to install the memcache or memcached client from pecl here http://pecl.php.net/package/memcache and here http://pecl.php.net/package/memcached respectively. do an install of memcachedb (will need a box with shell access), and start up the service.

client pseudo code would run something like this:

// get messages a user is part of

code...

// build an array of memcachedb keys from that list, set key to message_id to reference later

$keys = array();
foreach($mresult as $current){
    $keys[$current['message_id']] = 'messageread_'.$current['message_id'].'_'.$myid;
}

// using the "memcached" php client

$result = $memcacheobj->getMulti($keys);

getmulti returns an associative array in the struct of 'memcachekey' => 'memcachevalue'

// loop over result, removing found results from keys

foreach($keys as $key => $val){
    if(isset($result[$val])) unset($keys[$key]);
}

// cleanup to have array of unread messages, flip array to set values to message_id, reset array keys with array_values (optional)

$unread = array_values(array_flip($keys));

now you have an array of unread messages in the structure of:

array( 0 => 12, 1 => 23, etc... )

this is just a rough mockup of the process, im sure some cleaning up could be done to optimize this :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM