Class: Colossus::Engine::MemoryThreadSafe::ClientSessionStore

Inherits:
Object
  • Object
show all
Defined in:
lib/colossus/engines/memory_thread_safe/client_session_store.rb

Overview

Represents all the different sessions of a user. It can find

the global status of the user given all the different status.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl) ⇒ ClientSessionStore

Returns a new instance of ClientSessionStore.



10
11
12
13
14
15
16
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 10

def initialize(ttl)
  @sessions = ThreadSafe::Cache.new do |hash, key|
    hash[key] = Colossus::Engine::MemoryThreadSafe::ClientSession.new
  end
  @last_status = DISCONNECTED
  @ttl         = ttl
end

Instance Attribute Details

#last_statusObject (readonly)

Returns the value of attribute last_status.



7
8
9
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 7

def last_status
  @last_status
end

#sessionsObject

Returns the value of attribute sessions.



8
9
10
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 8

def sessions
  @sessions
end

#ttlObject (readonly)

Returns the value of attribute ttl.



7
8
9
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 7

def ttl
  @ttl
end

Instance Method Details

#[](session_id) ⇒ Object



41
42
43
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 41

def [](session_id)
  sessions[session_id]
end

#[]=(session_id, session_status) ⇒ Object



45
46
47
48
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 45

def []=(session_id, session_status)
  @last_status = status
  sessions[session_id].status = session_status
end

#delete(session_id) ⇒ Object



50
51
52
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 50

def delete(session_id)
  sessions.delete(session_id)
end

#delete_expired_sessionsObject



54
55
56
57
58
59
60
61
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 54

def delete_expired_sessions
  session_keys_to_delete = []
  sessions.each_pair do |session_id, session|
    session_keys_to_delete << session_id if (session.last_seen + ttl) < Time.now
  end

  session_keys_to_delete.each { |session_id| sessions.delete(session_id) }
end

#last_seenObject



31
32
33
34
35
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 31

def last_seen
  sessions.values.reduce(Time.new(0)) do |memo, session|
    session.last_seen > memo ? session.last_seen : memo
  end
end

#statusObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 18

def status
  sessions.values.reduce(DISCONNECTED) do |memo, session|
    case session.status
    when ACTIVE
      session.status
    when AWAY
      memo == ACTIVE ? memo : session.status
    else
      memo
    end
  end
end

#status_changed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/colossus/engines/memory_thread_safe/client_session_store.rb', line 37

def status_changed?
  last_status != status
end