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. 限制textarea的字数(包括复制粘贴)

    <textarea cols="100%" rows="10" id="tuconent" placeholder="评价( ...

  2. 从SQLite获取数据完成一个产品信息展示

    在ios实际开发当中,我们常常用到Core Data做为数据储存首选.但在处理一些大量复杂的数据值且数据之间相互关联的时候,这就不得不使用关系型数据库来实现.例如一个导航程序,自身应该包含大量的地图自 ...

  3. ShareSDK分享失败的原因

    关于分享估计很多都用的是ShareSDK的社会化分享,简单方便,支持的种类很多,但是一般的话都还是QQ,微信,新浪微博,腾讯微博为主. 最近需要导入一个分享的模块,失败了几次之后最终成功,分享给大家, ...

  4. JS模块化

    一.原始写法 /* 模块就是实现特定功能的一组方法. 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. 上面的函数m1()和m2(),组成一个模块.使用的时候,直接调用就行了. ...

  5. php抽奖代码

    1.经典概率算法抽奖 $tmpItems = ['电脑'=>10, '相机'=>50, '100元现金'=>500]; $proSum = array_sum($tmpItems); ...

  6. Oracle 11203 + ASM安装 for HP UX

    一,安装前准备 1.创建所需组和用户 /usr/sbin/groupadd -g 1000 oinstall/usr/sbin/groupadd -g 1020 asmadmin/usr/sbin/g ...

  7. (UWP开发)更为合理的一种ListView下拉刷新(PullToRefresh)实现方法

    最近在做的一个项目需要用到下拉刷新,但是参考了现在网络上比较普遍的方法,觉得都不太好,因为要在外部套上一个SrollViewer,容易出现滚动错误.于是刚开始的时候就把思路定到了ListView内部的 ...

  8. 前端项目通用、常用js common.js

    var url = location.href; if (url.toLowerCase().indexOf("/akweb_admin/") == -1) { function ...

  9. D3中动画(transition函数)的使用

    关于transition的几个基本点: 1. transition()是针对与每个DOM element的,每个DOM element的transition并不会影响其他DOM element的tra ...

  10. js整理5

    proto 每个对象具有的属性,指向构造该对象的构造函数的原型对象 prototype 函数的特有属性,指向原型对象:原型对象可以是对象,数组,函数等类型: constructor 原型对象和实例,都 ...