Class: Colossus::Engine::MemoryThreadSafe
- Inherits:
-
Object
- Object
- Colossus::Engine::MemoryThreadSafe
show all
- Includes:
- Observable
- Defined in:
- lib/colossus/engines/memory_thread_safe/memory.rb,
lib/colossus/engines/memory_thread_safe/client_session.rb,
lib/colossus/engines/memory_thread_safe/client_session_store.rb
Overview
Threadsage with ThreadSafe
Defined Under Namespace
Classes: ClientSession, ClientSessionStore
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of MemoryThreadSafe.
10
11
12
13
14
15
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 10
def initialize(ttl)
@client_sessions = ThreadSafe::Cache.new do |hash, key|
hash[key] = Colossus::Engine::MemoryThreadSafe::ClientSessionStore.new(ttl)
end
@ttl = ttl
end
|
Instance Attribute Details
#client_sessions ⇒ Object
Returns the value of attribute client_sessions.
7
8
9
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 7
def client_sessions
@client_sessions
end
|
#ttl ⇒ Object
Returns the value of attribute ttl.
7
8
9
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 7
def ttl
@ttl
end
|
Instance Method Details
#delete(user_id) ⇒ Object
60
61
62
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 60
def delete(user_id)
client_sessions.delete(user_id)
end
|
#delete_expired_users ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 80
def delete_expired_users
user_ids_to_delete = []
client_sessions.each_pair do |user_id, session_store|
user_ids_to_delete << user_id if (session_store.last_seen + ttl) < Time.now
end
user_ids_to_delete.each { |user_id| client_sessions.delete(user_id) }
end
|
#gc_ttl ⇒ Object
73
74
75
76
77
78
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 73
def gc_ttl
delete_expired_users
client_sessions.each_pair do |user_id, session_store|
session_store.delete_expired_sessions
end
end
|
#get(user_id) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 40
def get(user_id)
if client_sessions.key?(user_id)
{ user_id => client_sessions[user_id].status }
else
{ user_id => DISCONNECTED }
end
end
|
#get_all ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 52
def get_all
statuses = {}
client_sessions.each_pair do |user_id, session_store|
statuses[user_id] = session_store.status
end
statuses
end
|
#get_multi(*user_ids) ⇒ Object
48
49
50
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 48
def get_multi(*user_ids)
user_ids.inject({}) { |memo, user_id| memo.merge!(get(user_id)) }
end
|
#new_periodic_ttl ⇒ Object
68
69
70
71
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 68
def new_periodic_ttl
secs_ttl = Colossus.config.seconds_before_ttl_check
@periodic_ttl = EM::Synchrony.add_periodic_timer(secs_ttl, &method(:gc_ttl))
end
|
#reset! ⇒ Object
64
65
66
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 64
def reset!
client_sessions.clear
end
|
#set(user_id, client_id, given_status) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 22
def set(user_id, client_id, given_status)
if given_status == DISCONNECTED
client_sessions[user_id].delete(client_id)
else
client_sessions[user_id][client_id] = given_status
end
if client_sessions[user_id].sessions.empty? ||
client_sessions[user_id].status_changed?
status = client_sessions[user_id].status
delete(user_id) if status == DISCONNECTED
user_changed(user_id, status)
return true
end
false
end
|
#user_changed(user_id, status) ⇒ Object
17
18
19
20
|
# File 'lib/colossus/engines/memory_thread_safe/memory.rb', line 17
def user_changed(user_id, status)
changed
notify_observers(user_id, status)
end
|