php 会话变量:多少数据太多了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17554990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:06:22  来源:igfitidea点击:

Session Variables: How much data is too much?

phpmysqlsession-variables

提问by user1537360

I've only seen examples of session variables being used to store small amounts of data, like a single user id. I'm wondering if it would be more efficient to instead hold more frequently accessed data in the session variables to avoid querying the database.

我只见过会话变量用于存储少量数据(例如单个用户 ID)的示例。我想知道在会话变量中保存更频繁访问的数据以避免查询数据库是否会更有效。

For instance, I made a user class that gathers regularly requested data for that user upon construction (their user id, username, email, password and arrays of site specific data) and I hold this instance as a session variable. After the user's initial log in, the database rarely has to be queried to get information about the user because it's already in memory.

例如,我创建了一个用户类,该类在构建时为该用户收集定期请求的数据(他们的用户 ID、用户名、电子邮件、密码和站点特定数据数组),并将此实例作为会话变量保存。在用户初次登录后,很少需要查询数据库来获取有关用户的信息,因为它已经在内存中了。

Am I actually being more efficient at all, or am I just bogging down the system with memory usage?

我实际上是更有效率,还是我只是因为内存使用而使系统陷入困境?

Side note - I actually find it easier to grab data from the session instead of having to worry about optimising my queries and stuff, so I really hope I'm not being an idiot.

旁注 - 我实际上发现从会话中获取数据更容易,而不必担心优化我的查询和内容,所以我真的希望我不是一个白痴。

回答by nickhar

Firstly, PHP sessions aren't stored in memory by default, they are stored on disk, so every block/session you write to is going to occupy disk space and not memory (until you use PHP to read the session data).

首先,PHP 会话默认不存储在内存中,它们存储在磁盘上,因此您写入的每个块/会话都将占用磁盘空间而不是内存(直到您使用 PHP 读取会话数据)。

Yes, you're potentially being more efficient, but not if you want to scale and here's why:

是的,您可能会更有效率,但如果您想扩展则不然,原因如下:



Storing data in sessions

在会话中存储数据

It's perfectly acceptable to store somedata in sessions. Theoretically, there is no limit (although I've never tried to break it or even push it, just move to a more efficient solution). You will however, be limited by disk space and PHP memory_limit().

在会话中存储一些数据是完全可以接受的。理论上,没有限制(尽管我从未尝试打破它甚至推动它,只是转向更有效的解决方案)。但是,您将受到磁盘空间和 PHP 的限制memory_limit()

Often, data stored in sessions includes things like:

通常,会话中存储的数据包括以下内容:

  • Usernames
  • Hashes
  • Registration dates
  • Other variables (user group ids/keys etc)
  • Flash messages
  • (NOT passwords!)
  • 用户名
  • 哈希值
  • 注册日期
  • 其他变量(用户组 ID/密钥等)
  • 快讯
  • (不是密码!)

However, there is a tradeoff. If your traffic (and usage) increases and you're storing a lot of data in $_SESSION, you will very likely start to see problems, both in terms of disk and memory usage.

但是,有一个权衡。如果您的流量(和使用量)增加并且您在 中存储了大量数据$_SESSION,您很可能会开始看到磁盘和内存使用方面的问题。

I don't think there is any issue with what you're suggesting, but beyond the items you've listed and where the examples above overlap, care is required.

我认为您的建议没有任何问题,但除了您列出的项目以及上述示例重叠的地方之外,还需要小心。

If you want to scale (horizontally) and retain disk-based sessions then you have options (sticky sessionsor storage area network are a couple) as the disk on one server doesn't store the same sessions as a disk on another server.

如果您想扩展(水平)并保留基于磁盘的会话,那么您可以选择(粘性会话或存储区域网络是一对),因为一台服务器上的磁盘与另一台服务器上的磁盘存储的会话不同。



Session data location

会话数据位置

You can find the location where PHP stores session data by calling: session_save_path()

您可以通过调用找到 PHP 存储会话数据的位置: session_save_path()

or on the CLI:

或在 CLI 上:

php -r 'echo session_save_path(), "\n";'

You've not mentioned your OS, but common locations for the session files (across different OS types) are:

您没有提到您的操作系统,但会话文件(跨不同操作系统类型)的常见位置是:

/tmp 
/var/lib/php5/
/var/lib/php/session
c:/wamp/tmp

Sessions stored on disk usually have filenames that look like this using ls -al:

存储在磁盘上的会话通常具有如下所示的文件名ls -al

-rw-------  1 www www      0 2013-07-09 20:12 sess_bdsdjedmvtas5njhr5530b8rq6

It's worth noting that there are often garbage-collection processes that clean out dead sessions after specific periods. It does vary by OS, but they are usually present with various LAMP-based installs.

值得注意的是,通常有垃圾收集进程会在特定时间段后清除死会话。它确实因操作系统而异,但它们通常存在于各种基于 LAMP 的安装中。



Other session storage options/approaches

其他会话存储选项/方法

In your database
Session data is often stored in a DB instead of on local disk and this works well for both micro, small and (depending on how it's done) medium sites with a reasonable level of traffic.

在您的数据库中,
会话数据通常存储在数据库中而不是本地磁盘上,这对于具有合理流量水平的微型、小型和(取决于它的完成方式)中型站点都适用。

Like any other solution it has it's pro's and con's (like being able to ban/kick out a user by running a query rather than deleting a session file from /tmp)

像任何其他解决方案一样,它有其优点和缺点(例如能够通过运行查询来禁止/踢出用户,而不是从中删除会话文件/tmp

In memory
for larger, (higher traffic) sites and particularly where the volume of concurrent users is high, memory is quicker to read/write to for very frequently accessed variables or data instead of adding undue load to your DB. It can and should still be written to the DB (See write-through caching), but also be held in memory for efficient access.


更大(更高流量)站点的内存中,特别是在并发用户量很高的情况下,内存可以更快地读取/写入非常频繁访问的变量或数据,而不是向数据库增加过度负载。它可以而且应该仍然写入数据库(请参阅直写缓存),但也可以保存在内存中以进行高效访问。

One technique of particular merit is memory caching. A widely used example PHP-compatible open-source solution is Memcached, which can be used on one server or many [distributed]. I've seen this used by small firms as well as large and you only have to look at who uses it/contributes...

一种特别有价值的技术是内存缓存。一个广泛使用的 PHP 兼容开源解决方案示例是Memcached,它可以在一台服务器或多台[分布式]服务器上使用。我已经看到小公司和大公司都使用过它,你只需要看看谁使用它/贡献......

回答by Marcovecchio

It all depends on server resources, and on simultaneous users of your website / app.

这一切都取决于服务器资源,以及您的网站/应用程序的同时用户。

You can do a rough calculation, by estimating the average session memory each user will need, multiplying it by the average number of simultaneous visitors, and comparing this to the memory you have available for PHP in the server.

您可以粗略计算每个用户所需的平均会话内存,将其乘以同时访问的平均数量,然后将其与服务器中 PHP 可用的内存进行比较。

This calculation will help you estimate how much is too much in your scenario, in a very rough, but helpful way.

此计算将帮助您以一种非常粗略但很有帮助的方式来估计您的方案中有多少是过多的。

EDIT: by memory I mean RAM and/or disk space, depending on your setup.

编辑:内存是指 RAM 和/或磁盘空间,具体取决于您的设置。

回答by Bill Karwin

Some of the other answers assume you mean using PHP sessionsto cache data. This is a good solution to store ephemeral data that you need to be available from one request to the next.

其他一些答案假设您的意思是使用PHP 会话来缓存数据。这是一个很好的解决方案,用于存储您需要从一个请求到下一个请求可用的临时数据。

But I wonder if you mean using MySQL user-defined variables. You can store long strings of data in these variables, and they occupy RAM in the scope of your database thread on the MySQL server. But these user variables do notstay in memory after your session closes. They are not a good way to store data from one request to the next.

但我想知道您的意思是使用MySQL user-defined variables。您可以在这些变量中存储长字符串数据,它们在 MySQL 服务器上的数据库线程范围内占用 RAM。但是这些用户变量在您的会话关闭后不会保留在内存中。它们不是将数据从一个请求存储到下一个请求的好方法。

The only reason to store large amounts of data in user variables is that you need to use them in subsequent SQL queries during the same db session (which means during the same PHP request), and you're hoping to avoid transferring that string from the db server to your app. Otherwise, you might as well fetch the result and store it in a PHP variable.

在用户变量中存储大量数据的唯一原因是您需要在同一个数据库会话期间(这意味着在同一个 PHP 请求期间)的后续 SQL 查询中使用它们,并且您希望避免从db 服务器到您的应用程序。否则,您也可以获取结果并将其存储在 PHP 变量中。

Another solution for storing ephemeral data is to use memcached. You can store data directly, or you can use memcached as a backing store for your PHP session.

另一种存储临时数据的解决方案是使用memcached。您可以直接存储数据,也可以使用memcached 作为 PHP session 的后备存储

回答by Bahri Gungor

Try to keep in mind that the web paradigm encourages a stateless memory model. That is to say, load what you need to render the page, render the page, and release the resources.

请记住,Web 范式鼓励无状态内存模型。也就是加载你需要渲染的页面,渲染页面,释放资源。

Obviously there are times to cache data, but yes, storing information in session variables creates more memory usage for your application as a whole, and EACH SESSION will use whatever N bytes you are storing.

显然有时会缓存数据,但是是的,将信息存储在会话变量中会为整个应用程序创建更多的内存使用量,并且每个会话都将使用您存储的任何 N 个字节。

If you don't have a performance problem, don't worry about caching. On the other hand, if you want to cache and it's only on one server for a small number of users, don't fret it. Just be aware of the drawbacks of using session variables across a web farm (if you use one).

如果您没有性能问题,请不要担心缓存。另一方面,如果您想缓存并且它仅在一个服务器上供少数用户使用,请不要担心。请注意在网络场中使用会话变量的缺点(如果您使用)。