简体   繁体   English

存储1 MB字节数组作为会话属性

[英]Storing 1 MB byte array as session attribute

I am running a Java web app. 我正在运行Java Web应用程序。

A user uploads a file (max 1 MB) and I would like to store that file until the user completes an entire process (which consists of multiple requests). 用户上载一个文件(最大1 MB),我想存储该文件,直到用户完成整个过程(由多个请求组成)为止。

Is it ok to store the file as a byte array in the session until the user completes the entire process? 在用户完成整个过程之前,可以在会话中将文件存储为字节数组吗? Or is this expensive in terms of resources used? 还是在使用的资源上昂贵?

The reason I am doing this is because I ultimately store the file on an external server (eg aws s3) but I only want to send it to that server if the whole process is completed. 我这样做的原因是因为我最终将文件存储在外部服务器(例如aws s3)上,但是如果整个过程完成,我只想将其发送到该服务器。

Another option would be to just write the file to a temporary file on my server. 另一种选择是将文件写入服务器上的临时文件。 However, this means I would need to remove the file in case the user exits the website. 但是,这意味着如果用户退出网站,我将需要删除该文件。 But it seems excessive for me to add code to the SessionDestroyed method in my SessionListener which removes the file if it's just for this one particular case (ie: sessions are created throughout my entire application where I don't need to check for temp files). 但是对于我来说,将代码添加到我的SessionListenerSessionDestroyed方法中似乎过多了,如果只是针对这种情况(例如:在整个应用程序中都创建了会话,而我不需要检查临时文件),则该文件会删除该文件。 。

Thanks. 谢谢。

Maybe Yes, maybe No 可能是,可能不是

Certainly it is reasonable to store such data in memory in a session if that fits your deployment constraints. 当然,如果适合您的部署限制,则将此类数据存储在会话的内存中是合理的。

Remember that each user has their own session. 请记住,每个用户都有自己的会话。 So if all of your users have such a file in their session , then you must multiply to calculate the approximate impact on memory usage. 因此,如果所有用户的session中都有这样的文件,则必须乘以计算对内存使用量的近似影响。

If you exceed the amount of memory available at runtime, there will be consequences. 如果超出了运行时可用的内存量,则会有后果。 Your Servlet container may serialize less-used sessions to storage, which is a problem if you've not programmed all of your objects to support serialization. 您的Servlet容器可能会将使用较少的会话序列化到存储中,如果您尚未对所有对象进行编程以支持序列化,那么这将是一个问题。 The JVM and OS may use a swap file to move contents out of real memory as part of the virtual memory system. JVM和OS可以使用交换文件将内容移出真实内存,作为虚拟内存系统的一部分。 That swapping may impact or even cripple performance. 交换可能会影响甚至削弱性能。

You must consider your runtime deployment constraints, which you did not disclose. 您必须考虑未公开的运行时部署约束。 Are you running on a Raspberry Pi or inexpensive little cloud server with little memory available? 您是在Raspberry Pi还是运行在可用内存很少的廉价小型云服务器上? Or will you run on an enterprise-class server with half a terabyte of RAM? 还是要在具有0.5 TB内存的企业级服务器上运行? Do you have 3 users, 300, or 30,000? 您有3个用户,300个或30,000个用户吗? You need to crunch the numbers and determine your needs, and maybe do some runtime profiling to see actual usage. 您需要计算数字并确定您的需求,并可能需要进行一些运行时分析以查看实际使用情况。

For example… I write web apps using the Vaadin Framework , a sophisticated package for creating desktop-style apps within a web browser. 例如……我使用Vaadin Framework编写Web应用程序, Vaadin Framework是一个复杂的程序包,用于在Web浏览器中创建桌面样式的应用程序。 Being Servlet -based, Vaadin maintains a complete representation of each user's entire work data on the server-side in the Servlet session. 基于Servlet的Vaadin在Servlet会话的服务器端维护着每个用户整个工作数据的完整表示。 Multiplied by the number of users, and depending on the complexity of the app, this may require much memory. 乘以用户数量,并取决于应用程序的复杂性,这可能需要大量内存。 So I need to account for this and run my server on sufficient hardware with 64-bit Java tuned to run with a large amount of memory. 因此,我需要考虑这一点,并在足够的硬件上运行我的服务器,并将其调整为64位 Java以使其能够运行大量内存。 Or take other approaches such load-balancing across multiple servers with sticky sessions . 或者采用其他方法,例如通过粘性会话跨多个服务器进行负载平衡

Fortunately, RAM is quite cheap nowadays. 幸运的是,RAM现在很便宜。 And 64-bit hardware with large physical support for RAM modules , 64-bit operating systems, and 64-bit JVM implementations ( Azul , others ) are all readily available. 具备大量物理支持RAM模块 ,64位操作系统和64位JVM实现( Azul )的64位硬件也很容易获得。

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

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