我有一个很大的应用程序,有上千个活动会话。我想使用this迁移到Redis会话存储。理想情况下,我希望我当前的会话保持活跃。
有没有迁移活动会话的经验?我假设我编写了一个迁移或rake任务(我认为是迁移,所以我可以删除旧的表作为其中的一部分),并且我只想将所有当前的详细信息写入redis。
old_sessions = ActiveRecord::Base.connection.select_all("select * from sessions")
old_sessions.each { |session| $redis.set(????? ????) }但我担心数据的完整性。
发布于 2012-08-14 07:50:10
好了,经过一天的修改,我想出了以下几点:
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我把这个放在这里作为参考。如果你发现有什么问题,我洗耳恭听。
https://stackoverflow.com/questions/11940633
复制相似问题