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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?