Class: Ask::Agent::Persistence::Base
- Inherits:
-
Object
- Object
- Ask::Agent::Persistence::Base
- Defined in:
- lib/ask/agent/persistence/base.rb
Overview
Session persistence backed by a generic State::Adapter.
This wraps session data under namespaced keys so multiple sessions can coexist in the same state backend alongside other data.
Direct Known Subclasses
Constant Summary collapse
- KEY_PREFIX =
"ask:session:"- INDEX_KEY =
"ask:session:index"
Instance Attribute Summary collapse
-
#state ⇒ Ask::State::Adapter
readonly
The underlying state adapter.
Instance Method Summary collapse
-
#delete(session_id) ⇒ Object
Delete a session.
-
#initialize(state_adapter: nil) ⇒ Base
constructor
A new instance of Base.
-
#list ⇒ Array<String>
List all stored session IDs.
-
#load(session_id) ⇒ Hash?
Load a session.
-
#save(session_id, data) ⇒ Object
Persist a session.
Constructor Details
Instance Attribute Details
#state ⇒ Ask::State::Adapter (readonly)
Returns the underlying state adapter.
31 32 33 |
# File 'lib/ask/agent/persistence/base.rb', line 31 def state @state end |
Instance Method Details
#delete(session_id) ⇒ Object
Delete a session.
52 53 54 55 |
# File 'lib/ask/agent/persistence/base.rb', line 52 def delete(session_id) @state.delete(key(session_id)) @state.list_remove(INDEX_KEY, session_id) end |
#list ⇒ Array<String>
List all stored session IDs.
59 60 61 |
# File 'lib/ask/agent/persistence/base.rb', line 59 def list @state.list_range(INDEX_KEY, 0, -1) end |
#load(session_id) ⇒ Hash?
Load a session.
46 47 48 |
# File 'lib/ask/agent/persistence/base.rb', line 46 def load(session_id) @state.get(key(session_id)) end |
#save(session_id, data) ⇒ Object
Persist a session.
36 37 38 39 40 41 |
# File 'lib/ask/agent/persistence/base.rb', line 36 def save(session_id, data) @state.set(key(session_id), data) # Keep the index list unique — remove existing entry first then append @state.list_remove(INDEX_KEY, session_id) @state.list_append(INDEX_KEY, session_id) end |