Class: SmartPrompt::PersistenceLayer
- Inherits:
-
Object
- Object
- SmartPrompt::PersistenceLayer
- Defined in:
- lib/smart_prompt/persistence_layer.rb
Overview
PersistenceLayer handles saving and loading session data to/from disk
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#storage_path ⇒ Object
readonly
Returns the value of attribute storage_path.
Instance Method Summary collapse
-
#delete(session_id) ⇒ Object
Delete a session from disk.
-
#exists?(session_id) ⇒ Boolean
Check if a session exists on disk.
-
#initialize(config = {}) ⇒ PersistenceLayer
constructor
A new instance of PersistenceLayer.
-
#list_sessions ⇒ Object
List all session IDs stored on disk.
-
#load(session_id) ⇒ Object
Load a session from disk.
-
#save(session) ⇒ Object
Save a session synchronously.
-
#save_async(session) ⇒ Object
Save a session asynchronously.
-
#shutdown ⇒ Object
Stop the async writer gracefully.
Constructor Details
#initialize(config = {}) ⇒ PersistenceLayer
Returns a new instance of PersistenceLayer.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/smart_prompt/persistence_layer.rb', line 68 def initialize(config = {}) @backend = config[:backend] || :filesystem @storage_path = config[:storage_path] || "./history_data" @async_writer = AsyncWriter.new @enabled = config[:enabled] != false @async = config[:async] != false # Create storage directory if persistence is enabled ensure_storage_directory if @enabled end |
Instance Attribute Details
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
66 67 68 |
# File 'lib/smart_prompt/persistence_layer.rb', line 66 def enabled @enabled end |
#storage_path ⇒ Object (readonly)
Returns the value of attribute storage_path.
66 67 68 |
# File 'lib/smart_prompt/persistence_layer.rb', line 66 def storage_path @storage_path end |
Instance Method Details
#delete(session_id) ⇒ Object
Delete a session from disk
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/smart_prompt/persistence_layer.rb', line 125 def delete(session_id) return unless @enabled file_path = session_file_path(session_id) if File.exist?(file_path) File.delete(file_path) SmartPrompt.logger.info "Session #{session_id} deleted from disk" end rescue => e SmartPrompt.logger.error "Failed to delete session #{session_id}: #{e.}" end |
#exists?(session_id) ⇒ Boolean
Check if a session exists on disk
138 139 140 141 142 143 |
# File 'lib/smart_prompt/persistence_layer.rb', line 138 def exists?(session_id) return false unless @enabled file_path = session_file_path(session_id) File.exist?(file_path) end |
#list_sessions ⇒ Object
List all session IDs stored on disk
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/smart_prompt/persistence_layer.rb', line 146 def list_sessions return [] unless @enabled Dir.glob(File.join(@storage_path, "*.json")).map do |file| File.basename(file, ".json") end rescue => e SmartPrompt.logger.error "Failed to list sessions: #{e.}" [] end |
#load(session_id) ⇒ Object
Load a session from disk
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/smart_prompt/persistence_layer.rb', line 108 def load(session_id) return nil unless @enabled file_path = session_file_path(session_id) return nil unless File.exist?(file_path) data = File.read(file_path) session_data = deserialize_session(data) SmartPrompt.logger.info "Session #{session_id} loaded from #{file_path}" session_data rescue => e SmartPrompt.logger.error "Failed to load session #{session_id}: #{e.}" nil end |
#save(session) ⇒ Object
Save a session synchronously
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/smart_prompt/persistence_layer.rb', line 80 def save(session) return unless @enabled file_path = session_file_path(session.id) data = serialize_session(session) File.write(file_path, data) SmartPrompt.logger.info "Session #{session.id} saved to #{file_path}" rescue => e SmartPrompt.logger.error "Failed to save session #{session.id}: #{e.}" # Continue operating with in-memory storage (fallback behavior) end |
#save_async(session) ⇒ Object
Save a session asynchronously
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/smart_prompt/persistence_layer.rb', line 94 def save_async(session) return unless @enabled if @async @async_writer.enqueue do save(session) end else # If async is disabled, fall back to synchronous save save(session) end end |
#shutdown ⇒ Object
Stop the async writer gracefully
158 159 160 |
# File 'lib/smart_prompt/persistence_layer.rb', line 158 def shutdown @async_writer.stop if @async_writer end |