简体   繁体   English

Rails应用程序中的会话过多

[英]Too many sessions in a rails application

I am building an E-commerce website on ruby on rails from scratch.(This is my first project on ruby on rails) 我正在从头开始在Rails on Rails上构建一个电子商务网站(这是我的第一个Rails on Rails项目)。

My product belongs to a subcategory which in-turn belongs to a category. 我的产品属于一个子类别,而该子类别又属于一个类别。

My filters partial include multiple check-boxes for category,subcategory,additional_category(Like hand made clothes,factory built etc.),lifestyle(relaxed,corporate etc) and cloth_material_type(this has around 30 options ) 我的过滤器部分包含用于类别,子类别,additional_category(如手工衣服,工厂建造等),生活方式(放松,企业等)和cloth_material_type(具有大约30个选项 )的多个复选框

I am sending 5 arrays for each of these cases to the backend to search through the associations. 我将每种情况的5个数组发送到后端,以搜索关联。

Now when a non logged in user reloads the page the filters set by user resets to default . 现在,当非登录用户重新加载页面时,由用户设置的过滤器将重置为默认值

To avoid this I have four options in mind. 为避免这种情况,我想到了四个选择。

Option 1 . 选项1 Store the filter values set by the user in the cookies which is fast.But it might slow down the user's browser. 将用户设置的过滤器值快速存储在cookie中 ,但这可能会降低用户浏览器的速度。

Option2 . 选项2 Store the values in a session using ActiveRecord::SessionStore gem which will increase the size of session for me to 65K but would slow down the application. 使用ActiveRecord :: SessionStore gem将值存储在会话中,这对于我来说会将会话大小增加到65K,但会使应用程序变慢。

Option 3 .Using jquery modify/create document.url options so that every filter option gets appended to the document.url and on reload I get the parameters set by the user for filtering.But this looks very cumbersome to implement. 选项3。使用jquery 修改/创建document.url选项,以便将每个过滤器选项附加到document.url上,并在重新加载时获得用户设置的用于过滤的参数,但这实现起来非常麻烦。

Option 4 . 选项4 Using gems like rails temporary database etc. 使用宝石,例如Rails临时数据库等。

I have opted with option 2 and using session store for the purpose but I think that it will become cumbersome to maintain this in the future. 我选择了选项2并为此目的使用了会话存储,但是我认为将来要维护它会变得很麻烦。

Just need some suggestions like what do other rails ecommerce websites do to solve this problem or is there any better way to solve this. 只是需要一些建议,例如其他Rails电子商务网站可以解决此问题,或者有什么更好的方法可以解决此问题。

Redis Redis的

What I'd do is add a layer of abstraction; 我要做的是添加一层抽象; specifically I think you'd benefit from using Redis , or similar temporary db (as you alluded to in your question). 具体来说,我认为您将从Redis或类似的临时db中受益(如您在问题中所提到的)。

Redis is a key:value database, which basically stores JSON values for you to use within your app. Redis是一个key:value数据库,它基本上存储JSON值供您在应用程序中使用。 If you tie it to a model, you'll be able to store temporary values without hindering your app's performance. 如果将其绑定到模型,则可以存储临时值而不会影响应用程序的性能。

I think you could setup Redis to store a guest id, and an array of your values from that: 我认为您可以将Redis设置为存储来宾ID,以及一个来自其中的值数组:

[guest_user_id] => [
    1 => "x"
    2 => "y"
    3 => ["z", "a", "b"]
]

You'd be able to generate the guest_user_id when you initialize the Redis system, and store it in the user's session . 初始化Redis系统时,您将能够生成guest_user_id并将其存储在用户的session This way, you're only storing minimal data inside your user's browser, and can populate the various controller actions with Redis data: 这样,您只在用户的浏览器中存储了最少的数据,并且可以使用Redis数据填充各种控制器操作:

#config/routes.rb
resources :categories do
   resources :subcategories
end

#app/models/user.rb
Class User < ActiveRecord::Base
    def self.new_data
       # create guest_id and send to Redis
    end
end

This will allow you to populate a session with your guest_id if the user is not registered: 如果未注册用户,这将允许您使用guest_id填充会话:

#app/controllers/products_controller.rb
class ProductsController < ApplicationController
    def show
        @user = user_signed_in? ? current_user : User.new_data
        #You'll then be able to populate their Redis values with the data from the product selection etc
    end
end 

I could go into more specifics, but as you're only looking for suggestions, this is what I have to recommend at the moment 我可以介绍更多具体信息,但是由于您只是在寻找建议,因此,这是我目前要推荐的

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

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