在会话cookie中存储大量数据的含义是什么? [英] What are the implications of storing large amounts of data in the session cookie?

查看:109
本文介绍了在会话cookie中存储大量数据的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释在会话中存储大量数据的弊端或让我阅读一些东西吗?

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非常适合此任务,使用Google谷歌搜索会为您提供很好的资源,这些资源可以解释如何使用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.

这篇关于在会话cookie中存储大量数据的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆