Class: KStor::SessionStore

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

Overview

Collection of user sessions (in memory)

FIXME make it thread safe!

Instance Method Summary collapse

Constructor Details

#initialize(idle_timeout, life_timeout) ⇒ SessionStore

Returns a new instance of SessionStore.



37
38
39
40
41
42
# File 'lib/kstor/session.rb', line 37

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



44
45
46
47
48
# File 'lib/kstor/session.rb', line 44

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

#[](sid) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kstor/session.rb', line 50

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



64
65
66
67
68
69
# File 'lib/kstor/session.rb', line 64

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