简体   繁体   English

Rails-通过HTTP请求保存控制器实例变量数据

[英]Rails - save controller instance variable data across HTTP request

In my project, a user is uploading a text file, that needs to be read. 在我的项目中,用户正在上载需要阅读的文本文件。

File, can be of any size, the file I am using is 1 MB and has ~1500 lines. 文件,可以是任何大小,我正在使用的文件为1 MB,大约有1500行。 The file can be bigger as well. 文件也可以更大。 Hence instead of putting all in db, i thought of processing the file and retaining the data in instance variable. 因此,我考虑将文件处理并将数据保留在实例变量中,而不是将其全部放入db中。

But instance variables are not available across HTTP request. 但是实例变量在HTTP请求中不可用。 Hence what are the options available to me to retain the instance variable values across HTTP request .The other reason for not choosing DB was, I dont need the data to be persisted. 因此,我可以使用哪些选项来保留HTTP请求中的实例变量值 。不选择DB的另一个原因是,我不需要将数据持久化。 As long as user is logged in, data needs to be present for that time duration only. 只要用户登录,就只需要在该时间段内显示数据。 Once user is logged out, I can discard the data. 用户注销后,我可以丢弃数据。

Please let me know if you need further information. 如果您需要更多信息,请告诉我。

As @xyious advises, I would say avoid storing that much data in session, it is just not a good practice. 正如@xyious建议的那样,我要避免在会话中存储大量数据,这不是一个好习惯。 You could, however, do the following: 但是,您可以执行以下操作:

  1. Setup a system-wide configuration setting that holds a path where you store temporary files, in this case, the files uploaded by the user 设置系统范围的配置设置,该设置包含用于存储临时文件的路径,在这种情况下,临时文件是用户上传的文件
  2. Generate a random (maybe with SecureRandom.hex ) filename when the user uploads the file and store this file in the path mentioned on point #1 用户上载文件时,生成一个随机的文件名(也许使用SecureRandom.hex ),并将该文件存储在第1点提到的路径中
  3. Store this random filename in the user session, that way, even if you change between requests you can still access the filename 这样,即使在请求之间进行更改,也可以将随机文件名存储在用户会话中,但仍可以访问文件名
  4. On each request, whenever you need to process the data, pull the filename from the user's session and join the path of the setting of #1, read the file from the filesystem and do the processing as necessary 在每个请求上,每当需要处理数据时,都从用户会话中提取文件名并加入设置#1的路径,从文件系统中读取文件并根据需要进行处理
  5. Add a callback on your login/sessions controller so that when a user logs out you go and find the filename and delete it before logging out, that way you don't keep unused files around 在登录/会话控制器上添加回调,这样当用户注销时,您可以查找文件名并在注销前将其删除,这样就不会保留未使用的文件

I would advise against it, but you could store the data in a session variable, or in a cookie. 我建议不要这样做,但是您可以将数据存储在会话变量或cookie中。
Why would you need that much data to be stored while the user is logged in ? 为什么在用户登录时需要存储那么多数据? Is it possible to just save important bits ? 是否可以只保存重要的位?

Using instance variables to store content is not a right approach since you don't have a limit on the size of file uploaded and you end up passing the data everytime. 使用实例变量存储内容不是正确的方法,因为您对上传的文件大小没有限制,并且最终每次都会传递数据。

Firstly, decide something on the size limit since you expect text file from users and then upload the file temporarily with a reference path in DB. 首先,确定大小限制,因为您希望用户收到文本文件,然后使用DB中的参考路径临时上传文件。 This file can be cleaned up when required and will make accessing the content simple. 可以在需要时清除此文件,并使访问内容更简单。 To further improve this, enable caching mechanism and setup a caching server for the uploaded files. 为了进一步改善这一点,请启用缓存机制并为上传的文件设置缓存服务器。

If you are not fine with this then other option i can think of is using session variables which is already suggested. 如果您对此不满意,那么我可以想到的其他选择是使用已经建议的会话变量。 So this data will stay per session which fits your requirement. 因此,此数据将在每个会话中保留,这符合您的要求。 you can just session[:file_Data] = "put parsed content here" 您可以session[:file_Data] = "put parsed content here"

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

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