我有一个Rails应用程序,它将会话信息存储在数据库中,用户信息存储在“用户”表中,其中“id”作为主键,并且自动增加。
登录后,用户可以转到“产品”页面,其中显示按ID过滤的产品:
@products = Rails.cache.fetch("user-products-" + session[:id].to_s, {:expires_in => 30.minutes}) do
Product.where(:user_id => session[:id])
end
发生了令人困惑的事情:有时用户可以看到其他用户的产品,显然是随意的。我已经检查过产品表中的用户ID大约10次了,根本就没有意义。我存储在memcache中的其他内容也是如此,例如用户名和姓,地址,设置......
这种情况随机发生,并不一致。可能是在有大量流量时(用户报告在高峰时段发生这种情况)。
在我的config / application.rb中:
config.cache_store = :dalli_store, "memcached.mydomain.com", { :namespace => "Frontend", :pool_size => 10 }
在config / initializers / session_store.rb
中Rails.application.config.session_store :active_record_store, key: '_ssbl_session', :expire_after => 7.days
我使用Ruby 2.3.1,Rails 4.2.5,Dalli gem v 2.7.6 with connection_pool 2.2.1和activerecord-session_store 0.1.2,Apache 2.4.6,Passenger 5.0.26,memcached 1.4.15 on CentOS 7。
我尝试过的事情:
我已经检查了答案:Users take sessions of other users when sessions are stored in memcached (Rails)。添加该代码时,我得到500.
任何帮助都将不胜感激。
稍后编辑:
我在用户登录后设置会话[:id]:
def process_login
...
# at this point I have authenticated the user by e-mail and pwd and have the object in the "user" var
session[:id] = user.id
...
end
编辑3月10日: 我不再将任何会话数据存储到memcache中。我从问题中删除了“dalli”和“memcache”标签。问题依然存在。我仍然无法重现这一点,但我可以看到它是真实的,因为受此问题影响的会话日志包含对他们无法访问的控制器的命中。