简体   繁体   English

Ruby on Rails 4,将会话存储在单独的数据库中

[英]Ruby on Rails 4, store session in separate database

I have 2 rails applications (version 4.0 and 4.1) which have their own database and a secound db-connector to the other database. 我有2个Rails应用程序(版本4.0和4.1),它们具有自己的数据库和与另一个数据库的第二数据库连接器。 Both apps have a login-form and now I want to share the session. 这两个应用程序都有一个登录表单,现在我想共享会话。

In both apps I added the activerecord-session_store gem and in the config/initializers/seesion_store.rb file I set session_store to active_record_store. 在这两个应用程序中,我都添加了activerecord-session_store gem,并且在config / initializers / seesion_store.rb文件中,将session_store设置为active_record_store。

How can I tell one app that it use the other database (not the default db-connector) to store the session? 如何告诉一个应用程序它使用另一个数据库(不是默认的db-connector)来存储会话?

Is there a way to share sessions with cookies? 有没有办法与Cookie共享会话? Both apps have different URLs. 两个应用程序都有不同的URL。

I'm looking forward to your reply. 我期待着您的回复。

You may override the session store class with your own class and have the functionality to write and read sessions from your second database. 您可以使用自己的类覆盖会话存储类,并具有从第二个数据库写入和读取会话的功能。

Example class: 示例类:

https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/sql_bypass.rb https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/sql_bypass.rb

Here, you can have connection return your second db connection. 在这里,您可以让connection返回第二个数据库连接。

Referring to doc: 参考文档:

https://github.com/rails/activerecord-session_store https://github.com/rails/activerecord-session_store

You may provide your own session class implementation, whether a feature-packed Active Record or a bare-metal high-performance SQL store, by setting 您可以通过设置来提供自己的会话类实现,无论是功能丰富的Active Record还是裸机高性能SQL存储

ActionDispatch::Session::ActiveRecordStore.session_class = MySessionClass

You must implement these methods: 您必须实现以下方法:

self.find_by_session_id(session_id)
initialize(hash_of_session_id_and_data, options_hash = {})
attr_reader :session_id
attr_accessor :data
save
destroy

The example SqlBypass class is a generic SQL session store. 示例SqlBypass类是通用SQL会话存储。 You may use it as a basis for high-performance database-specific stores. 您可以将其用作高性能特定于数据库的存储的基础。

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

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