doupai5450 2014-02-15 02:52
浏览 686
已采纳

PHP,计算和显示每日,每周和每月统计数据

I am a newbie with PHP and therefore this is more of a conceptual question or maybe even a question about 'best practices'.

Often, I see websites with stats drawn from their database. For example, let's say it is a sales lead website. It may have stats at the top of the page like:

NEW SALES LEADS YESTERDAY: 123

NEW SALES LEADS THIS MONTH: 556

NEW SALES LEADS THIS YEAR: 3870

Obviously, this should not be calculated everytime the page is displayed, right? That would potentially be a large burden on the server? How do people cache this type of data. Any best practices? I thought I writing a CRON jobs that would calculate it on a daily basis and insert to a database. What are your ideas? Thank you!

  • 写回答

2条回答 默认 最新

  • dongqiongzheng0615 2014-02-15 03:02
    关注

    You can calculate it once and then store it in a xcache. Here, however there doesn't seem to be a need for a cron. The query can run one time and store the result in xcache. Important thing here would be to set the expiration time of the stored value according to your use case. For eg. if you need to store daily stats like above, set the expiration time to be a few hours. In case of data which gets updated every minute, you can set the expiration time to be a few minutes.

    Something like this.

    $newSalesLeadYest;
    if(xcache_isset("newSalesLeadYest")){
        $newSalesLeadYest = xcache_get("newSalesLeadYest");
    } else{
        $newSalesLeadYest = runQueryToFetchStat();
        //Cache set for X secs
        xcache_set("newSalesLeadYest", $newSalesLeadYest, X);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器