DbSession¶ ↑
Simplify the task of storing temporary data in the database.
Getting started¶ ↑
Add DbSession to your Gemfile:
gem 'db_session'
Run the bundle command to install it.
After you install DbSession and add it to your Gemfile, you need to run the generator:
rails generate db_session:install
The generator will install an initializer which describes DbSession configuration options.
DbSession uses Sidekiq to perform some asynchronous task. Sidekiq uses Redis to manage its job queue, so you’ll need to install Redis.
Configuration¶ ↑
You can set up the life span of a session in config/initializers/db_session.rb:
config.session_validity = 60 * 60 * 24 * 2
This value represent the time in seconds after which a session expire and can potentially be deleted from the database. The expiration of a session is counted from the time it was last modified.
Usage¶ ↑
You can call this methods on any controller or view:
set_db_session(key, object)
This method overwrite the content of the session with the key–object pair.
add_to_db_session(key, object)
This method add the key–object pair to the content of the session.
get_from_db_session(key)
This method return the object stored in the session for the specified key. If no key is specified it returns the whole content of the session.
clear_db_session
This method clear the content of the session.
Objects are sotred in the database as JSON strings, so in order to be stored an object need to support JSON serialization and deserialization.