Class: Legion::Gaia::SessionStore
- Inherits:
-
Object
- Object
- Legion::Gaia::SessionStore
- Defined in:
- lib/legion/gaia/session_store.rb
Defined Under Namespace
Classes: Session
Instance Method Summary collapse
- #find_or_create(identity:) ⇒ Object
- #get(session_id) ⇒ Object
-
#initialize(ttl: 86_400) ⇒ SessionStore
constructor
A new instance of SessionStore.
- #prune_expired ⇒ Object
- #remove(session_id) ⇒ Object
- #size ⇒ Object
- #touch(session_id, channel_id: nil) ⇒ Object
Constructor Details
#initialize(ttl: 86_400) ⇒ SessionStore
Returns a new instance of SessionStore.
15 16 17 18 19 20 |
# File 'lib/legion/gaia/session_store.rb', line 15 def initialize(ttl: 86_400) @sessions = {} @identity_index = {} @ttl = ttl @mutex = Mutex.new end |
Instance Method Details
#find_or_create(identity:) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/legion/gaia/session_store.rb', line 22 def find_or_create(identity:) @mutex.synchronize do session_id = @identity_index[identity] if session_id && @sessions[session_id] session = @sessions[session_id] return session unless expired?(session) remove_unlocked(session_id) end session = Session.new(identity: identity) @sessions[session.id] = session @identity_index[identity] = session.id session end end |
#get(session_id) ⇒ Object
57 58 59 |
# File 'lib/legion/gaia/session_store.rb', line 57 def get(session_id) @mutex.synchronize { @sessions[session_id] } end |
#prune_expired ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/legion/gaia/session_store.rb', line 69 def prune_expired @mutex.synchronize do expired_ids = @sessions.select { |_, s| expired?(s) }.keys expired_ids.each { |id| remove_unlocked(id) } expired_ids.size end end |
#remove(session_id) ⇒ Object
61 62 63 |
# File 'lib/legion/gaia/session_store.rb', line 61 def remove(session_id) @mutex.synchronize { remove_unlocked(session_id) } end |
#size ⇒ Object
65 66 67 |
# File 'lib/legion/gaia/session_store.rb', line 65 def size @mutex.synchronize { @sessions.size } end |
#touch(session_id, channel_id: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/legion/gaia/session_store.rb', line 39 def touch(session_id, channel_id: nil) @mutex.synchronize do session = @sessions[session_id] return nil unless session history = channel_id ? (session.channel_history + [channel_id]).uniq : session.channel_history updated = Session.new( id: session.id, identity: session.identity, channel_history: history, created_at: session.created_at, last_active_at: Time.now.utc ) @sessions[session_id] = updated updated end end |