好吧,经过一天的修改,这就是我想出的:
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
我把这个放在这里作为参考。或者,如果您发现它有问题,我会全神贯注。