Class: HighLevel::Storage::Memory
- Defined in:
- lib/high_level/storage/memory.rb
Overview
Thread-safe in-memory SessionStorage. Default storage when no other backend is configured. Ported from vendor/highlevel-api-sdk/lib/storage/memory-session-storage.ts; uses a Monitor instead of JS’s single-threaded model.
Constant Summary collapse
- TIMESTAMP_KEYS =
Internal bookkeeping keys stripped from sessions on read.
%i[created_at updated_at].freeze
Constants inherited from Base
Instance Method Summary collapse
-
#delete_session(resource_id) ⇒ void
Remove a stored session.
-
#disconnect ⇒ void
Release any resources held by the backend.
-
#get_access_token(resource_id) ⇒ String?
Fetch just the access token from a stored session.
-
#get_session(resource_id) ⇒ Hash?
Fetch a stored session.
-
#init ⇒ void
Initialize the backend (open connections, ensure schema, …).
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
-
#session_count ⇒ Integer
The number of sessions currently stored.
-
#set_client_id(client_id) ⇒ void
Set the OAuth client id; its first hyphen-separated segment becomes the application namespace for stored sessions.
-
#set_session(resource_id, session_data) ⇒ void
Store (or replace) a session, stamping
:expire_at. -
#storage_active? ⇒ Boolean
Whether #init has run without a subsequent #disconnect.
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
15 16 17 18 19 20 21 |
# File 'lib/high_level/storage/memory.rb', line 15 def initialize super @monitor = Monitor.new @sessions = {} @client_id = nil @initialized = false end |
Instance Method Details
#delete_session(resource_id) ⇒ void
This method returns an undefined value.
Remove a stored session.
73 74 75 76 |
# File 'lib/high_level/storage/memory.rb', line 73 def delete_session(resource_id) @monitor.synchronize { @sessions.delete(unique_key(resource_id)) } nil end |
#disconnect ⇒ void
This method returns an undefined value.
Release any resources held by the backend. Memory’s disconnect also clears all stored sessions.
30 31 32 33 34 35 |
# File 'lib/high_level/storage/memory.rb', line 30 def disconnect @monitor.synchronize do @sessions.clear @initialized = false end end |
#get_access_token(resource_id) ⇒ String?
Fetch just the access token from a stored session.
67 68 69 70 |
# File 'lib/high_level/storage/memory.rb', line 67 def get_access_token(resource_id) session = get_session(resource_id) session && session[:access_token] end |
#get_session(resource_id) ⇒ Hash?
Fetch a stored session.
57 58 59 60 61 62 63 64 |
# File 'lib/high_level/storage/memory.rb', line 57 def get_session(resource_id) @monitor.synchronize do document = @sessions[unique_key(resource_id)] return nil if document.nil? document.except(*TIMESTAMP_KEYS) end end |
#init ⇒ void
This method returns an undefined value.
Initialize the backend (open connections, ensure schema, …).
24 25 26 |
# File 'lib/high_level/storage/memory.rb', line 24 def init @monitor.synchronize { @initialized = true } end |
#session_count ⇒ Integer
Returns the number of sessions currently stored.
79 80 81 |
# File 'lib/high_level/storage/memory.rb', line 79 def session_count @monitor.synchronize { @sessions.size } end |
#set_client_id(client_id) ⇒ void
This method returns an undefined value.
Set the OAuth client id; its first hyphen-separated segment becomes the application namespace for stored sessions.
38 39 40 41 42 |
# File 'lib/high_level/storage/memory.rb', line 38 def set_client_id(client_id) raise ArgumentError, "client_id is required" if client_id.nil? || client_id.to_s.empty? @monitor.synchronize { @client_id = client_id.to_s } end |
#set_session(resource_id, session_data) ⇒ void
This method returns an undefined value.
Store (or replace) a session, stamping :expire_at.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/high_level/storage/memory.rb', line 45 def set_session(resource_id, session_data) data = symbolize_keys(session_data) document = data.merge( expire_at: calculate_expire_at(data[:expires_in]), created_at: Time.now, updated_at: Time.now ) @monitor.synchronize { @sessions[unique_key(resource_id)] = document } nil end |
#storage_active? ⇒ Boolean
Returns whether #init has run without a subsequent #disconnect.
85 86 87 |
# File 'lib/high_level/storage/memory.rb', line 85 def storage_active? @monitor.synchronize { @initialized } end |