4

I have a large application that as many, many thousand active sessions. I want to migrate into a Redis session store using this. And ideally, I want my current sessions to stay active.

Does anyone have any experience in migrating active sessions. I assume I write either a migration or a rake task (I think migration, so I can drop the old table as part of this), and I want to just write into redis all the current details.

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

But I am worried about data integrity.

4

1 に答える 1

13

了解しました。これをハッキングして1日過ごした後、私が思いついたのは次のとおりです。

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

これを参考にしています。または、何か問題がある場合は、私はすべての耳です。

于 2012-08-13T23:50:10.373 に答える