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

rails4.0 session activerecord的更多相关文章

  1. 解决rails4.0中send_file文件下载两次的问题

    之前在开发文件下载的功能时,我遇到了一个很奇怪的问题,点击下载链接,在chrome console中会出现两次请求,第一次返回200,下载的数据缓存在chrome的cache中,第二次返回304,直接 ...

  2. asp.net 2.0 Session丢失问题

    可行的解决方法(本人已用): 1.Web.config文件修改sessionstate模式(默认为InProc) <sessionState mode="/> 2.开启ASP.N ...

  3. cas4.0 session中返回更多的用户信息

    实现思路: 新增AccoutAttributeDao类继承StubPersonAttributeDao,重写getPerson方法.实际应用中我们只需要修改getPersion方法中的内容,根据实际情 ...

  4. 远程线程注入突破SESSION 0

    远程线程注入突破SESSION 0 SESSION 0 隔离 在Windows XP.Windows Server 2003,以及更老版本的Windows操作系统中,服务和应用程序使用相同的会话(Se ...

  5. ruby -- 进阶学习(三)Strong Parameters在rail3.0和4.0中的区别

    今天coding的时候遇到一个未知的类型,于是用puts logo_params.class查了下数据类型,然后google了一下发现是 Strong Parameter Strong paramet ...

  6. rails 4.0.2 + mongoid 对mongodb进行增删改查

    新建项目 rails new mongoid_app --skip-active-record --skip-test-unit --skip-bundle create create README. ...

  7. ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value:

    今天碰到一个相当棘手的问题,那就是ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value . 本来在本地测试是没有任 ...

  8. zookeeper源码分析之六session机制

    zookeeper中session意味着一个物理连接,客户端连接服务器成功之后,会发送一个连接型请求,此时就会有session 产生. session由sessionTracker产生的,sessio ...

  9. Openfire的启动过程与session管理

    说明   本文源码基于Openfire4.0.2.   Openfire的启动       Openfire的启动过程非常的简单,通过一个入口初始化lib目录下的openfire.jar包,并启动一个 ...

随机推荐

  1. xml in SQL

    几年前,学习html时,顺便把xml也学了哈,知道了xpath和xquery的概念,可都没去落实,下面这篇文章,可以学习学习 http://www.cnblogs.com/huyong/archive ...

  2. 使用VBScript实现设置系统环境变量的小程序

    本人有点桌面洁癖,桌面上只放很少的东西,很多软件都用快捷键调出.最近频繁用到一个软件,我又不想放个快捷方式在桌面,也不想附到开始菜单,于是乎想将其所在目录附加到系统环境变量Path上,以后直接在运行中 ...

  3. Denormalization 2

    In a relational database, denormalization is an approach to speeding up read performance (data retri ...

  4. app后端设计--总目录 (转)

    特此说明,我转载的!!! app后端设计(1)--api app后端设计(2)--xmpp的使用 app后端设计(3)--短信,邮件,推送服务 app后端设计(4)-- 通讯的安全性 app后端设计( ...

  5. ASP.Net Core 里是如何把一个普通的 Action 返回类型转换为某种 IActionResult 的

    秘密在于这个类型: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker 在它的 CreateActionResult 方法里会将相关类型 ...

  6. Swift 基本常量和变量,基本数据类型

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4dbf56 } p.p2 { margin: 0.0px 0. ...

  7. [leetcode] 小心成环

    156. Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes wit ...

  8. apache 服务器配制

    简介:Apache 是世界上使用量第一的Web服务器软件,可用于linux,unix,windows等平台,尤其是对Linux支持完美 Apache的优点: 功能强大,自带很多功能模块,可根据需求编译 ...

  9. 使用IntelliJ IDEA 配置Maven(入门)【转】

    1.在IntelliJ IDEA中配置maven 打开-File-Settings  2.新建maven WEB项目 打开-File-New-Project 点击NEXT  点击NEXT  添加的配置 ...

  10. ASP.NET中GridView数据导出到Excel

    /// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></para ...