dsc862009 2014-12-30 09:31
浏览 13
已采纳

如何设置新闻事件/公告在PHP中过期? [关闭]

PHP newbie here. My question is related to the logic/best practice for executing a certain task.

I have a news event/announcement being displayed on a website and needs to be removed/expire after a certain date/time. I have set a expiration date/time in the database, when a user visits the website after the date/time has passed a query is triggered and the news event/announcement is set to "0" (which is hidden).

I would like to know if this the best practice of accomplishing it or is there a better way?

Thanks.

  • 写回答

2条回答 默认 最新

  • dongpu42006096 2014-12-30 09:41
    关注

    The method you mention is usually effective, especially for small applications, but not a best practice. The reason it's not a best practice is because:

    Issues

    1. You are making the user wait for task execution
    2. If there is no activity on your website, these tasks will not be done
    3. If anything happens while executing a task, your user will receive the errors

    The first two might not seem to matter much, but that's only because the tasks you have now are very fast and non-critical. However, if a task would take a second or two, that would mean the user now has to wait 2 extra seconds before he sees the page, which is bad.

    Likewise, if nobody visits your site for a week and there's a list of 15 tasks that need to be done, the user now would have to wait for 30 seconds. It might even time out the whole page; which would mean your tasks are now unfinished and the user is annoyed with getting a timeout for seemingly no reason.

    In addition, if one of your tasks is time critical, it still won't be done. For example if the task is to send someone a reminder email after 24 hours but nobody logs in, the mail won't be sent.

    The last one is also a problem; both because this makes it hard to see when a task fails (as the error is logged as a user problem, if at all) and because your user (again for no reason) is now looking at an error screen.

    Solution

    If you want to use the best practice, move all these sorts of tasks to either a Scheduled Task (under windows) or a Cronjob (under unix). This means you have a system service that periodically starts up and executes a PHP script that can do maintenance to your site, such as removing these news messages, sending out emails, or other things.

    This has a number of advantages:

    1. The server will always be there on time to run the tasks when they need to be run
    2. You can disable timeouts and upgrade memory availability to run intensive tasks
    3. Users will not have to wait for anything to complete
    4. You can add special logging to the server, so that you know when these important but hidden tasks fail

    Most providers allow you to set these kinds of tasks even on cheap hosting packages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)