Class: Innate::Session

Inherits:
Object
  • Object
show all
Includes:
Optioned
Defined in:
lib/innate/session.rb,
lib/innate/session/flash.rb

Overview

Mostly ported from Ramaze, but behaves lazy, no session will be created if no session is used.

We keep session data in memory until #flush is called, at which point it will be persisted completely into the cache, no question asked.

You may store anything in here that you may also store in the corresponding store, usually it’s best to keep it to things that are safe to Marshal.

The Session instance is compatible with the specification of rack.session.

Since the Time class is used to create the cookie expiration timestamp, you will have to keep the ttl in a reasonable range. The maximum value that Time can store on a 32bit system is:

Time.at(2147483647) # => Tue Jan 19 12:14:07 +0900 2038

The default expiration time for cookies and the session cache was reduced to a default of 30 days. This was done to be compatible with the maximum ttl of MemCache. You may increase this value if you do not use MemCache to persist your sessions.

Defined Under Namespace

Classes: Flash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Optioned

included

Constructor Details

#initialize(request, response) ⇒ Session

Returns a new instance of Session.



50
51
52
53
54
55
# File 'lib/innate/session.rb', line 50

def initialize(request, response)
  @request, @response = request, response
  @cookie_set = false
  @cache_sid = nil
  @flash = Flash.new(self)
end

Instance Attribute Details

Returns the value of attribute cookie_set.



48
49
50
# File 'lib/innate/session.rb', line 48

def cookie_set
  @cookie_set
end

#flashObject (readonly)

Returns the value of attribute flash.



48
49
50
# File 'lib/innate/session.rb', line 48

def flash
  @flash
end

#requestObject (readonly)

Returns the value of attribute request.



48
49
50
# File 'lib/innate/session.rb', line 48

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



48
49
50
# File 'lib/innate/session.rb', line 48

def response
  @response
end

Instance Method Details

#clearObject



73
74
75
76
# File 'lib/innate/session.rb', line 73

def clear
  cache.delete(sid)
  @cache_sid = nil
end

#delete(key) ⇒ Object



69
70
71
# File 'lib/innate/session.rb', line 69

def delete(key)
  cache_sid.delete(key)
end

#fetch(key, value = nil) ⇒ Object Also known as: []



64
65
66
# File 'lib/innate/session.rb', line 64

def fetch(key, value = nil)
  cache_sid[key]
end

#flush(response = @response) ⇒ Object

Additional interface



80
81
82
83
84
85
86
# File 'lib/innate/session.rb', line 80

def flush(response = @response)
  return if !@cache_sid or @cache_sid.empty?

  flash.rotate!
  cache.store(sid, cache_sid, :ttl => options.ttl)
  set_cookie(response)
end

#sidObject



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

def sid
  @sid ||= cookie || generate_sid
end

#store(key, value) ⇒ Object Also known as: []=

Rack interface



59
60
61
# File 'lib/innate/session.rb', line 59

def store(key, value)
  cache_sid[key] = value
end