weixin_33682790 2014-07-12 11:29 采纳率: 0%
浏览 13

PHP和MySQL实时提要

I'm attempting to build a live-updated system in php-mysql (and jQuery). The question I have is if the approach i'm using is good/correct or if i should look into another way of doing it:

Using jQuery and AJAX i have made:

setInterval(function() {
  Ajax('check_status.php');
},4000);

in check_status.php i use memcache, to check if the result is either 1 or 0

$memcache = new Memcache;
$memcache->connect('127.1.0.1', 11211) or die ("");
$userid.$key = md5($userid."live_feed");
$result = $memcache->get($userid.$key);
if($result==1) {
  doSomething();
}

The idea is that user A does something, and that updates the memcache for user B. The memcache is then being checked every 4 seconds via jQuery, and that way i can do a live_feed to user B.

The reason i use memcache is to limit the mysql_queries and thereby limiting the load on the datbase.

So the question is. Am i totally off here ? Is there a better way of doing this, to reduce the server load ?

Thank you

  • 写回答

2条回答 默认 最新

  • weixin_33734785 2014-07-15 00:14
    关注

    I'd rather use a version id for content. For example say the user A's content is retrieved and the version id is set as 1000. This is then saved in memcached with the key userA_1000. If user B's action or any other action affects the user A's content, the version ID is then reset to 1001. When you next time checks memcached, you will now check for the key userA_1001, which does not exist and will tell you that it's updated. Hence re-create the content, save in memcached and send back with the Ajax request. User version key can also be saved in memcached in order to not to do a DB query each time. So

    User A's key/val --- user_a_key => userA_1001
    User A's content --- userA_1001 => [Content]
    

    When a change has happaned that effected the user A's content, simply change the version and update the key of the content in memcahed

    User A's key/val --- user_a_key => userA_1002
    

    So next time your Ajax request look for the users content in memcached, it will notice that there is nothing save for the key userA_1002, which will prompt it to re-create the content. If it's found simply respond saying no need to update anything.

    Use a well designed class methods to handle when and how the content is updated and how the keys are invalidated.

    评论

报告相同问题?