可以在$ _SESSION中保存很多信息吗? [英] Is it okay to save lots of information in $_SESSION?

查看:143
本文介绍了可以在$ _SESSION中保存很多信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在$_SESSION中存储许多数组,以防止从MySQL检索信息.可以吗$_SESSION中的太多"信息是多少?或者没有任何太多"信息?谢谢.

I need to store many arrays in $_SESSION to prevent retrieving information from MySQL. Is it okay? How much is "too much" information in $_SESSION or there isn't any "too much"? Thanks.

P.S.或者最好使用 http://php.net/manual/en/book.memcache.php ?

P.S. Or it's better to use http://php.net/manual/en/book.memcache.php ?

推荐答案

您可以在会话中存储的数据限制受会话存储层的限制.默认会话存储为文件系统,一个会话存储为一个文件.会话变量/数组键的名称及其数据以序列化形式存储.管道符号将变量名和值彼此分隔.

The limit of data you can store inside a session is limited by the session storage layer. The default sessions store is the file-system and one session is stored into one file. The name of the session variable/array-key is stored as well as it's data in a serialized form. A pipe symbol separates variable names and values from each other.

如果您要存储带有字符串的数组,那么文件的大小将比字符串的长度加上键的长度大,元数据的额外开销以及变量名的大小也将相类似.

If you're storing arrays with strings, then the file will be similar large than the length of the strings plus the length of the keys and a little overhead for meta data as well as the size of the variable names.

文件的大小受文件系统限制.例如,在EXT3中,每个文件16 GB.因此,这是一个太多"的问题.在会话中存储的数据量不能超过存储层允许的数据量.

The size of a file is limited by the file-system. For example, in EXT3 this are 16 Gigabyte per file. So this is one "too much". You can not store more data into a session than the storage layer allows you to.

我能想到的下一个限制是您的记忆力.由于PHP需要将文件中的数据加载到内存中,并在请求结束时将其从内存中存储到文件中.因此,如果您在PHP中具有内存限制那么这实际上将限制您的会话大小.例如,PHP 5.2中的标准内存限制为16MB,但这可能会因您的安装而异.

The next limit I can think of is the one of your memory. As PHP needs to load the data from the file into the memory and stores it from memory to the file at the end of the request. So if you've got a memory limit in PHP then this will actually limit as well the size of your session. For example, there is a standard memory limit of 16MB in PHP 5.2, but this may vary with your installation.

仅将整个内存用于会话并没有多大意义.

Using the whole memory for the session only does not make much sense by the way.

除了这些硬限制之外,可能还有性能限制,这些限制与配置请求的数量,硬盘的速度等有关.

Next to these hard limits there might be performance limits which are related to the number of congurent requests, how fast your harddisk is etc.

由于您的问题很短,我认为到目前为止您还没有遇到任何具体问题,所以我认为这超出了范围.例如.如果您确实不需要,则使用memcached只是开销.除了讨论设计决策(会话中永远不要缓存)外,这些决策通常根本无法回答.

As your question is pretty short I assume you didn't run into any concrete problems so far, so I think it would be out of scope. E.g. using memcached if you don't really need is would be only overhead. As well as discussing design decisions (never cache in sessions) which can not be answered in general at all.

每个会话100或200千字节(在系统上找到会话目录,并实际查看将要成为文件的大小)不会破坏程序.按照建议,您应该注意,不再需要的旧会话文件将在一段时间后自动删除.

The 100 or 200 Kilobyte per session (locate the session directory on your system and take an actual look how large the files are becomming) should not break your program. As suggested you should take care that old session files that aren't needed any longer get removed after a certain period of time automatically.

要了解有关使用PHP进行会话配置的更多信息,请参见以下页面中的会话运行时配置PHP手册.

To learn more about your session configuration in PHP please see Session Runtime Configuration in the PHP Manual.

这篇关于可以在$ _SESSION中保存很多信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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