Skip to content

Commit

Permalink
Store sessions in binary (25% space saving, and same speed to load/st…
Browse files Browse the repository at this point in the history
…ore)
  • Loading branch information
brianhempel committed Jul 25, 2011
1 parent bed7ba2 commit 12a194b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/mongo_session_store/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def save

collection.save(
:_id => @_id,
:data => @data,
:data => BSON::Binary.new(@data),
:created_at => @created_at,
:updated_at => @updated_at
)
Expand Down Expand Up @@ -132,12 +132,12 @@ def destroy_session(env, session_id, options)
end

def pack(data)
[Marshal.dump(data)].pack("m*")
Marshal.dump(data)
end

def unpack(packed)
return nil unless packed
Marshal.load(packed.unpack("m*").first)
Marshal.load(StringIO.new(packed.to_s))
end

end
Expand Down
6 changes: 3 additions & 3 deletions lib/mongo_session_store/mongo_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Session
include MongoMapper::Document
set_collection_name MongoSessionStore.collection_name
key :_id, String
key :data, String, :default => [Marshal.dump({})].pack("m*")
key :data, Binary, :default => Marshal.dump({})
timestamps!
end

Expand Down Expand Up @@ -69,12 +69,12 @@ def destroy_session(env, session_id, options)
end

def pack(data)
[Marshal.dump(data)].pack("m*")
Marshal.dump(data)
end

def unpack(packed)
return nil unless packed
Marshal.load(packed.unpack("m*").first)
Marshal.load(StringIO.new(packed.to_s))
end

end
Expand Down
6 changes: 3 additions & 3 deletions lib/mongo_session_store/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Session

identity :type => String

field :data, :type => String, :default => [Marshal.dump({})].pack("m*")
field :data, :type => BSON::Binary, :default => BSON::Binary.new(Marshal.dump({}))
end

# The class used for session storage.
Expand Down Expand Up @@ -73,12 +73,12 @@ def get_session_model(env, sid)
end

def pack(data)
[Marshal.dump(data)].pack("m*")
BSON::Binary.new(Marshal.dump(data))
end

def unpack(packed)
return nil unless packed
Marshal.load(packed.unpack("m*").first)
Marshal.load(StringIO.new(packed.to_s))
end

end
Expand Down

0 comments on commit 12a194b

Please sign in to comment.