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.

    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?