Class: Nitro::Session

Inherits:
Hash
  • Object
show all
Includes:
Expirable
Defined in:
lib/nitro/session.rb

Overview

A web application session.

State is a neccessary evil but session variables should be avoided as much as possible. Session state is typically distributed to many servers so avoid storing complete objects in session variables, only store oids and small integer/strings.

The session should be persistable to survive server shutdowns.

The session can be considered as a Hash where key-value pairs are stored. Typically symbols are used as keys. By convention uppercase symbols are used for internal Nitro session variables (ie :FLASH, :USER, etc). User applications typically use lowercase symbols (ie :cart, :history, etc).

– TODO: rehash of the session cookie TODO: store -> cache, reimplement helpers. ++

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ Session

Create the session for the given context. If the hook method ‘created’ is defined it is called at the end. Typically used to initialize the session hash.



158
159
160
161
162
# File 'lib/nitro/session.rb', line 158

def initialize(context = nil)
  @session_id = create_id
  expires_after(Session.keepalive)
  created if respond_to?(:created)
end

Class Attribute Details

.cacheObject Also known as: store

The sessions cache (store).



81
82
83
# File 'lib/nitro/session.rb', line 81

def cache
  @cache
end

Instance Attribute Details

#session_idObject (readonly)

The unique id of this session.



151
152
153
# File 'lib/nitro/session.rb', line 151

def session_id
  @session_id
end

Class Method Details

.countObject

The number of active (online) sessions. DON’T use yet!



122
123
124
# File 'lib/nitro/session.rb', line 122

def count
  Session.cache.size
end

.currentObject

Returns the current session from the context thread local variable.



143
144
145
# File 'lib/nitro/session.rb', line 143

def current
  Context.current.session
end

.garbage_collectObject Also known as: gc!

Perform Session garbage collection. You may call this method from a cron job.



129
130
131
132
133
134
135
136
137
# File 'lib/nitro/session.rb', line 129

def garbage_collect
  expired = []
  for s in Session.cache.all
    expired << s.session_id if s.expired?
  end
  for sid in expired
    Session.cache.delete(sid)
  end
end

.lookup(context) ⇒ Object

Lookup the session in the cache by using the session cookie value as a key. If the session does not exist creates a new session, inserts it in the cache and appends a new session cookie in the response.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nitro/session.rb', line 97

def lookup(context)
  if session_id = context.cookies[Session.cookie_name]
    session = Session.cache[session_id]
  end

  unless session
    # Create new session.
    session = Session.new(context)
    cookie = Cookie.new(Session.cookie_name, session.session_id)
    if Session.cookie_expires
      cookie.expires = Time.now + Session.keepalive 
    end
    context.add_cookie(cookie)
    Session.cache[session.session_id] = session 
  else
    # Access ('touch') the existing session.
    session.touch!
  end
  
  return session
end

.setup(type = Session.cache_type) ⇒ Object

Load the correct Session specialization according to the cache type.



87
88
89
90
# File 'lib/nitro/session.rb', line 87

def setup(type = Session.cache_type)
  # gmosx: RDoc friendly. 
  require 'nitro/session/' + type.to_s
end

Instance Method Details

#syncObject Also known as: restore

Synchronize the session store, by restoring this session. Especially useful in distributed and/or multiprocess setups.



168
169
170
# File 'lib/nitro/session.rb', line 168

def sync
  Session.cache[@session_id] = self
end

#touch!Object



173
174
175
# File 'lib/nitro/session.rb', line 173

def touch!
  expires_after(Session.keepalive)
end