lib/merb/session/merb_memory_session.rb in merb-0.3.4 vs lib/merb/session/merb_memory_session.rb in merb-0.3.7
- old
+ new
@@ -2,47 +2,92 @@
module SessionMixin
def setup_session
MERB_LOGGER.info("Setting up session")
- before = @cookies[_session_id_key]
- @session , @cookies[_session_id_key] = Merb::MemorySession.persist(@cookies[_session_id_key])
- @_new_cookie = @cookies[_session_id_key] != before
+ before = @_cookies[_session_id_key]
+ @_session , @_cookies[_session_id_key] = Merb::MemorySession.persist(@_cookies[_session_id_key])
+ @_new_cookie = @_cookies[_session_id_key] != before
end
def finalize_session
MERB_LOGGER.info("Finalize session")
- set_cookie(_session_id_key, @cookies[_session_id_key], Time.now+Merb::Const::WEEK*2) if @_new_cookie
+ set_cookie(_session_id_key, @_session.session_id, _session_expiry) if (@_new_cookie || @_session.needs_new_cookie)
end
end
class MemorySession
-
- class << self
-
- RAND_CHARS = [*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z']
+ attr_accessor :session_id
+ attr_accessor :data
+ attr_accessor :needs_new_cookie
+
+ def initialize(session_id)
+ @session_id = session_id
+ @data = {}
+ end
+
+ class << self
# Generates a new session ID and creates a row for the new session in the database.
def generate
- rand_max = RAND_CHARS.size
- sid = (0...32).inject("") { |ret,_| ret << RAND_CHARS[rand(rand_max)] }
- [create(:session_id => sid, :data => {}), sid]
+ sid = Merb::SessionMixin::rand_uuid
+ MemorySessionContainer[sid] = new(sid)
end
-
+
# Gets the existing session based on the <tt>session_id</tt> available in cookies.
# If none is found, generates a new session.
def persist(session_id)
if session_id
- session = self[session_id]
- sid = session_id
+ session = session = MemorySessionContainer[session_id]
end
unless session
- session, sid = generate
+ session = generate
end
- [session, sid]
+ [session, session.session_id]
end
+
+ end
+
+ # Regenerate the Session ID
+ def regenerate
+ new_sid = Merb::SessionMixin::rand_uuid
+ old_sid = @session_id
+ MemorySessionContainer[new_sid] = MemorySessionContainer[old_sid]
+ @session_id = new_sid
+ MemorySessionContainer.delete(old_sid)
+ self.needs_new_cookie=true
+ end
+
+ # Recreates the cookie with the default expiration time
+ # Useful during log in for pushing back the expiration date
+ def refresh_expiration
+ self.needs_new_cookie=true
+ end
+
+ # Lazy-delete of session data
+ def delete
+ @data = {}
+ end
+
+ def [](key)
+ @data[key]
+ end
+
+ def []=(key, val)
+ @data[key] = val
+ end
+
+ # Has the session been loaded yet?
+ def loaded?
+ !! @data
+ end
+
+ end
+
+ class MemorySessionContainer
+ class << self
def setup(opts={})
@opts = opts
@sessions = Hash.new
@timestamps = Hash.new
@@ -53,18 +98,19 @@
end
def create(opts={})
self[opts[:session_id]] = opts[:data]
end
+
def [](key)
@mutex.synchronize {
@timestamps[key] = Time.now
@sessions[key]
}
end
-
+
def []=(key, val)
@mutex.synchronize {
@timestamps[key] = Time.now
@sessions[key] = val
}
@@ -96,11 +142,10 @@
end
def sessions
@sessions
end
-
+
end # end singleton class
-
- end # end DRbSession
+ end # end MemorySessionContainer
end
\ No newline at end of file