加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails:从Active Record Session Store迁移到R

发布时间:2020-12-17 01:50:02 所属栏目:百科 来源:网络整理
导读:我有一个很大的应用程序,有多少千个活动会话.我想使用 this迁移到Redis会话存储.理想情况下,我希望我当前的会话保持活动状态. 有没有人有迁移活动会话的经验.我假设我写了一个迁移或rake任务(我认为迁移,所以我可以删除旧表作为其中的一部分),我想写入redis
我有一个很大的应用程序,有多少千个活动会话.我想使用 this迁移到Redis会话存储.理想情况下,我希望我当前的会话保持活动状态.

有没有人有迁移活动会话的经验.我假设我写了一个迁移或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

我把它作为参考.或者,如果你看到它有问题,我会全力以赴.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读