• rails4.0 session activerecord


    Active Record Session Store

    A session store backed by an Active Record class. A default class is provided, but any object duck-typing to an Active Record Session class with text session_id and data attributes is sufficient.

    Installation

    Include this gem into your Gemfile:

    gem 'activerecord-session_store', github: 'rails/activerecord-session_store'
    

    Run the migration generator:

    rails generate active_record:session_migration
    

    Then, set your session store in config/initializers/session_store.rb:

    Foo::Application.config.session_store :active_record_store
    

    Configuration

    The default assumes a sessions tables with columns:

    • id (numeric primary key),
    • session_id (string, usually varchar; maximum length is 255), and
    • data (text or longtext; careful if your session data exceeds 65KB).

    The session_id column should always be indexed for speedy lookups. Session data is marshaled to the data column in Base64 format. If the data you write is larger than the column's size limit, ActionController::SessionOverflowError will be raised.

    You may configure the table name, primary key, and data column. For example, at the end of config/application.rb:

    ActiveRecord::SessionStore::Session.table_name = 'legacy_session_table'
    ActiveRecord::SessionStore::Session.primary_key = 'session_id'
    ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data'
    

    Note that setting the primary key to the session_id frees you from having a separate id column if you don't want it. However, you must set session.model.id = session.session_id by hand! A before filter on ApplicationController is a good place.

    Since the default class is a simple Active Record, you get timestamps for free if you add created_at and updated_at datetime columns to the sessions table, making periodic session expiration a snap.

    You may provide your own session class implementation, whether a feature-packed Active Record or a bare-metal high-performance SQL store, by setting

    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. You may use it as a basis for high-performance database-specific stores.

    http://rubydoc.info/gems/activerecord-session_store/0.1.0/frames

  • 相关阅读:
    树套树+【UVALive】6709 Mosaic 二维线段树
    汇编实验1. 计算1+2+3+…+10,将结果显示在屏幕上。4
    Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) D. Destruction of a Tree
    HDU 4417 Super Mario主席树
    spoj+B
    2018-2019赛季多校联合新生训练赛第五场(2018/12/14)补题题解
    迷宫问题 POJ
    浅谈二分搜索与二分查找
    Moving Tables POJ
    Humidex POJ
  • 原文地址:https://www.cnblogs.com/heimirror/p/3536965.html
Copyright © 2020-2023  润新知