在会话 cookie 中存储大量数据意味着什么?意味着什么、数据、cookie

2023-09-06 05:04:28 作者:爱她久伴她@

谁能解释在会话中存储大量数据的缺点或指出一些阅读材料?

Could anyone explain disadvantages of storing large amounts of data within the session or point me to some reading ?

如果在会话中存储数据和从数据文件中读取数据之间有什么区别,我也会感兴趣?

I would also be interested if there is any difference between storing data in a session and reading data from datafiles ?

推荐答案

如果你在一个会话中存储大量数据,你的输入/输出性能会下降,因为会有大量的读/写.

If you store a large amount of data within a session, you'll have input/output performance drops since there's going to be a lot of reading/writing.

默认情况下,PHP 中的会话存储在平面文件的/tmp 目录中.因此,您的会话数据被写入某种数据文件中.

Sessions in PHP, by default, are stored in /tmp directory in a flat file. Therefore, your session data is written in a data-file of some sort.

PHP 允许您通过 session_set_save_handler() 函数覆盖其默认会话处理程序,您可以在其中重新定义读取/写入/维护会话的方式.

PHP allows you to override its default session handler by session_set_save_handler() function where you can redefine the way sessions are read / written / maintained.

您也可以通过 php.ini 文件覆盖它,您可以在其中通过 session.save_handler 指令指定它.

You can also override this via php.ini file where you specify it via session.save_handler directive.

现在,拥有大量会话存储大量数据的含义是,将创建大量文件,并且由于硬盘驱动器的运行方式(当然是机械的,它们是常见的仍然).你拥有的越多,找到它所需的时间就越长.它们越大,阅读它所需的时间就越长.如果您有很多并且它们很大 - 麻烦加倍,则需要改变方法.

Now, the implication of having a large number of sessions storing large data is that a lot of files will be created and it will take some time to find them due to the ways hard drives operate (mechanical ones of course, which are the common ones still). The more you have, the longer it takes to find it. The larger they are, longer it takes to read it. If you have a lot of them and they are large - double the trouble, a change in approach is needed.

那么解决办法是什么?

通常,当遇到性能下降时,人们会对其网站进行负载平衡.不幸的是,这不适用于会话,因为负载平衡正在选择使用哪台计算机来服务当前请求.这意味着不同的计算机将为您在某些网站上浏览的页面提供服务.这意味着,如果这些计算机使用默认的会话存储机制(/tmp 目录),则不会在服务器之间保留会话,因为它们无法访问彼此的/tmp 目录.您可以通过安装 NAS 并使其对集群中的所有计算机全局可见来解决此问题,但这既昂贵又难以维护.

Usually, when met with performance drop - people load balance their websites. That doesn't work with sessions unfortunately because load balancing is choosing which computer to use that will serve the current request. That means that different computers will serve pages you browse at some website. Which means, if those computers use default mechanism of session storage (/tmp directory), the sessions will not be preserved across the servers since they cannot access each other's /tmp directory. You can solve this by mounting a NAS and making it globally visible to all of the computers in the cluster, but that's both expensive and difficult to maintain.

另一种选择是将会话存储在数据库中.可以从我们虚构集群中的任何计算机访问数据库.通常,有专门用于处理会话的数据库,专门用于与存储您的网站内容或其他任何内容的数据库分开.在 NoSQL 流行的时代——在我看来,NoSQL 非常适合处理会话.它们易于扩展,将数据写入存储设备的速度比 RDBMS 快.

The other option is to store the sessions in a database. A database is accessible from any of the computers in our fictional cluster. Usually, there are specialised databases used for handling sessions, specialised in sense of being separate from the database storing your website content or whatever. In the time of NoSQL popularity - in my opinion, NoSQL is great for handling sessions. They scale easily, they are faster in writing the data to storage devices than RDBMSs are.

第三种选择是提升所有这些,放弃硬盘作为永久存储解决方案,只使用服务器的内存进行会话存储.您得到的是令人难以置信的性能,但是您的所有 RAM 可能很快就会消失.您还可以创建一个计算机集群,将会话存储在其 RAM 中.Redis 和 Memcache 非常适合这项任务,谷歌搜索一下将为您提供很好的资源来解释如何使用 Redis 或 Memcache 来存储会话.

Third option is to boost all of this, ditch hard drives as permanent storage solution and just use your server's memory for session storage. What you get is incredible performance, however all your RAM might be quickly gone. You can also create a cluster of computers that store sessions in their RAM. Redis and Memcache are great for this task, googling a bit will give you good resources that explain how to use Redis or Memcache to store sessions.

所有这一切的底线是:不要在会话中存储太多数据.根据您的需求和预算 - 有 3 种可供选择的方式来存储和使用会话.

Bottom line of all this is: don't store too much data in your sessions. According to your needs and budget - there are 3 options available how to store and work with sessions.