Class: KStor::SessionStore

Inherits:
Object
  • Object
show all
Defined in:
lib/kstor/session.rb

Overview

Collection of user sessions (in memory).

Concurrent accesses are synchronized on a mutex.

Instance Method Summary collapse

Constructor Details

#initialize(idle_timeout, life_timeout) ⇒ KStor::SessionStore

Create new session store.

Parameters:

  • idle_timeout (Integer)

    sessions that aren’t updated for this number of seconds are considered invalid

  • life_timeout (Integer)

    sessions that are older than this number of seconds are considered invalid



60
61
62
63
64
65
# File 'lib/kstor/session.rb', line 60

def initialize(idle_timeout, life_timeout)
  @idle_timeout = idle_timeout
  @life_timeout = life_timeout
  @sessions = {}
  @sessions.extend(Mutex_m)
end

Instance Method Details

#<<(session) ⇒ Object

Add a session to the store.

Parameters:



70
71
72
73
74
# File 'lib/kstor/session.rb', line 70

def <<(session)
  @sessions.synchronize do
    @sessions[session.id] = session
  end
end

#[](sid) ⇒ KStor::Session?

Fetch a session from it’s ID.

Parameters:

  • sid (String)

    session ID to lookup

Returns:

  • (KStor::Session, nil)

    session or nil if session ID was not found or session has expired



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kstor/session.rb', line 81

def [](sid)
  @sessions.synchronize do
    s = @sessions[sid.to_s]
    return nil if s.nil?

    if invalid?(s)
      @sessions.delete(s.id)
      return nil
    end

    s.update
  end
end

#purgeObject

Delete expired sessions.



96
97
98
99
100
101
# File 'lib/kstor/session.rb', line 96

def purge
  now = Time.now
  @sessions.synchronize do
    @sessions.delete_if { |_, s| invalid?(s, now) }
  end
end