会话变量:多少数据太多? [英] Session Variables: How much data is too much?

查看:72
本文介绍了会话变量:多少数据太多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

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).

是的,您可能会更有效率,但如果要扩展,则没有效率,这就是原因:

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


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

It's perfectly acceptable to store some data 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().

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

Often, data stored in sessions includes things like:

  • 用户名
  • 哈希
  • 注册日期
  • 其他变量(用户组ID/密钥等)
  • Flash消息
  • (没有密码!)

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

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.

我认为您的建议没有任何问题,但是除了您列出的内容以及上面的示例重叠的地方,还需要注意.

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 sessions or storage area network are a couple) as the disk on one server doesn't store the same sessions as a disk on another server.


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

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

或在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

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

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

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

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

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.


在您的数据库中
会话数据通常存储在数据库中,而不是存储在本地磁盘上,这对于流量适中的微型站点,小型站点(取决于完成方式)均适用.

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.

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

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)

内存中
对于较大的(较高流量)站点,尤其是在并发用户数量很大的站点,对于访问频繁的变量或数据,内存的读取/写入速度更快,而不是给数据库增加了不必要的负载.它可以并且仍然应该写入数据库(请参阅直写式缓存),但也应保留在内存中以进行有效访问.

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.

一种特别有价值的技术是内存缓存. Memcached 是一个广泛使用的与PHP兼容的示例开放源代码解决方案,它可以在一台服务器上使用,也可以在许多[分布式]服务器上使用.我已经看到了小型企业和大型企业都在使用此功能,而您只需要查看谁使用它/进行贡献即可.

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...

这篇关于会话变量:多少数据太多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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