简体   繁体   English

PHP - 在$ _SESSION或数据库中存储大会话数组?

[英]PHP - to store big session array in $_SESSION or in database?

I need to store a big array during the session (currently up to few kB, and I'd limit it to 0.25MB max). 我需要在会话期间存储一个大数组(目前高达几KB,我将其限制为最大0.25MB)。

In your opinion and practice, is it better to store it in $_SESSION or in database? 在您的意见和实践中,将它存储在$ _SESSION或数据库中是否更好?

Speed matters, but so does processor/memory usage, as it's on shared host, and I wouldn't want them to shut the site down for resource overuse. 速度很重要,但处理器/内存使用情况也是如此,因为它位于共享主机上,我不希望它们因为资源过度使用而关闭网站。

Would you say there's a size range in which $_SESSION can be used with confidence it will work well? 你会说有一个尺寸范围,其中$ _SESSION可以放心使用吗? (For example 0kb-100kB or whatever your practice/tests showed). (例如0kb-100kB或您的练习/测试显示的任何内容)。

Thanks. 谢谢。

0.25MB with a sane number of sessions will use less resources if stored in the Session, than in the DB. 如果存储在会话中,具有合理数量的会话的0.25MB将使用比在DB中更少的资源。 So the likelyhood of resource overusage is lower with the session. 因此会话中资源过度的可能性较低。

It depends a lot in the number of concurrent users of your site and your server. 它取决于您的站点和服务器的并发用户数。 Since it is a shared server i would use a database if (and only if) you have a very big number of users, but its easier and faster with $_SESSION, and 200kbs its not a lot. 由于它是一个共享服务器,我会使用数据库,如果(并且仅当)你有非常多的用户,但它更容易和更快与$ _SESSION,200kbs不是很多。 Also, not using a DB saves you a lot of time retrieving data since it doesnt have to go back and forth DB server and web server on each request. 此外,不使用数据库可以节省大量的时间来检索数据,因为它不必在每个请求上来回转发数据库服务器和Web服务器。

The real performance penalty with sessions is that PHP rewrites the session data for every request. 会话的实际性能损失是PHP重写每个请求的会话数据。 Writing to disk (which it will do in moste cases) is very slow. 写入磁盘(它将在moste情况下执行)非常慢。 It should be used only for simple things like authentication and small data structures eg shopping carts and the like. 它应仅用于简单的事情,如身份验证和小型数据结构,例如购物车等。

Depending on what kind of data it is and what software you have available on the server you should store it in the database or you could use a NoSQL solution like MongoDB, Redis or CouchDB. 根据它是什么类型的数据以及服务器上可用的软件,您应该将它存储在数据库中,或者您可以使用NoSQL解决方案,如MongoDB,Redis或CouchDB。

Since you are considering using sessions in the first place, I take it as consistency of the data is not the number one priority. 由于您正在考虑首先使用会话,因此我认为数据的一致性不是第一优先级。 If the data is important you should use the MySQL database since it follows the ACID principles and will keep your data even after a client disassociates itself with the current session. 如果数据很重要,您应该使用MySQL数据库,因为它遵循ACID原则,即使在客户端与当前会话解除关联后,也会保留您的数据。

If the consistency is not important, consider using Memcached if it is available. 如果一致性不重要,请考虑使用Memcached(如果可用)。

Summary : Use a database, but not necessarily MySQL (depending on what data it is). 简介 :使用数据库,但不一定是MySQL(取决于它是什么数据)。

Sessions are loaded into memory usually after being stored in a session file on the file system when you use the default session handler. 当您使用默认会话处理程序时,会话通常在存储在文件系统上的会话文件中后加载到内存中。 You don't have persisted memory issues with sessions unless you are are explicitly using memory to store your sessions. 除非您明确使用内存来存储会话,否则会话中没有持久的内存问题。 In my opinion it is bad to have large sessions anyway. 在我看来,无论如何都要举行大型会议。 There has to be some fundamental flaw in the design. 设计中必须存在一些根本性缺陷。 If you want to relate data to a user this is usually achieved by designing your database such that data is associated to the correct users simply through foreign keys. 如果要将数据与用户关联,通常可以通过设计数据库来实现,这样数据就可以通过外键与正确的用户相关联。 You have the ability to query a small subset of this data rather than loading a large chunk of data into memory and filtering it. 您可以查询此数据的一小部分,而不是将大量数据加载到内存中并对其进行过滤。 Sessions are only really useful for user authentication. 会话仅对用户身份验证非常有用。 A RESTful API will not use sessions at all. RESTful API根本不会使用会话。 I should probably note that I am biased in favour of stateless web. 我应该注意到,我偏向于无国籍网络。 Sessions persist state between requests. 会话在请求之间保持状态。 I've only accepted authentication as a valid use case because browsers do not provide a versatile and secure alternative 我只接受身份验证作为有效的用例,因为浏览器不提供通用且安全的替代方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM