dongzhouzhang8696 2015-09-08 13:51
浏览 47
已采纳

将大数据存储在会话变量中是一种好习惯吗?

I am currently programming a php site, which atm needs to query a large amount of data (about 4 - 5MB) everytime. I already have a session going and wanted to ask, if its good practice to store that data in the session variable?

The current plan is to also maintain a table in the Database containing when a table has changed last. If that timestamp would be newer, then the data would be queried again, if not, use the data of the session variable as its still consistent...

Is this a good way to avoid querying too much data? And what speed impacts would the site have when a session is about 5MB in size?

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • doxd96148 2015-09-08 13:57
    关注

    It's not really good practice (it will make PHP chew far more memory than it really should), but I'm not sure how it will affect performance.

    I suppose the real question is this: Why do you need to store so much in the session? If it's information that is meant to be accessible between sessions, then you should be storing it in a database and loading it 'at need'.

    If it's binary data (images, files, etc.) that are only relevant while the session is valid, then store it in a temporary file for the user (look at tempnam() and sys_get_temp_dir()), then store the temporary filename on the session.

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

报告相同问题?