简体   繁体   中英

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

But instance variables are not available across HTTP request. 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. 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. 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
  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
  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.
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. 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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