ruby-on-rails - rails : Moving from Active Record Session Store to a Redis Store

标签 ruby-on-rails session redis

我有一个大型应用程序,有数千个事件 session 。我想使用 this 迁移到 Redis session 存储中.理想情况下,我希望我当前的 session 保持活跃。

有没有人有迁移事件 session 的经验。我假设我编写了迁移或 rake 任务(我认为是迁移,所以我可以删除旧表作为其中的一部分),我只想将所有当前详细信息写入 redis。

old_sessions = ActiveRecord::Base.connection.select_all("select * from sessions")
old_sessions.each { |session| $redis.set(????? ????) }

但我担心数据完整性。

最佳答案

好吧,经过一天的努力,这就是我想出的:

class MoveActiveRecordSesionsIntoRedis < ActiveRecord::Migration
  def up
    #get all the sessions from the last month
    old_sessions = ActiveRecord::Base.connection.select_all("select * from sessions where updated_at > '#{Time.now - 1.month}'")

    old_sessions.each do |session|


      #convert the base64 data back into the object
      data = ActiveRecord::SessionStore::Session.unmarshal(session["data"])

      #load each session into Redis, dumping the object appropriately      
      $redis.setex session["session_id"], 
                   1.month.to_i, 
                   Marshal.dump(data).to_s.force_encoding(Encoding::BINARY)
    end

    #drop the old session table (So long unecessary 3Gigs!)
    drop_table :sessions
  end

  def down
    raise ActiveRecord::IrreversibleMigration, "Session face-plant!"
  end
end

我把它放在这里作为引用。或者,如果您发现它有问题,我会洗耳恭听。

关于ruby-on-rails - rails : Moving from Active Record Session Store to a Redis Store,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11940633/

相关文章:

session - 如何在grails中实现cookie、用户跟踪?

php - jquery 和 php ajax session

Redis 键卡在 -1 的 TTL

redis - Redis 上的 FLUSHALL 和 FLUSHDB 命令返回 "unk command"

ruby-on-rails - 如何修复 Ruby View 中的 'NoMethodError' 错误

javascript - 过滤器黑名单

php - 为什么在 php 上使用 web 框架(如 rails)?

java - hibernate session.save() 不反射(reflect)在数据库中

php - 尝试为 Windows 编译 phpredis

ruby-on-rails - 为什么 Capistrano 会这样?