class Aws::SessionStore::DynamoDB::Locking::Base
Handles session management.
Public Class Methods
@param [Aws::SessionStore::DynamoDB::Configuration] cfg
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 7 def initialize(cfg) @config = cfg end
Public Instance Methods
Deletes session based on id
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 29 def delete_session(env, sid) handle_error(env) do @config.dynamo_db_client.delete_item(delete_opts(sid)) end end
Retrieves session data based on id
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 24 def get_session_data(env, sid) raise NotImplementedError end
Updates session in database
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 12 def set_session_data(env, sid, session, options = {}) return false if session.empty? packed_session = pack_data(session) handle_error(env) do save_opts = update_opts(env, sid, packed_session, options) @config.dynamo_db_client.update_item(save_opts) sid end end
Private Instance Methods
Attributes to be retrieved via client
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 146 def attr_opts { attributes_to_get: ['data'], consistent_read: @config.consistent_read } end
Attributes to update via client.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 93 def attr_updts(env, session, add_attrs = {}) data = data_unchanged?(env, session) ? {} : data_attr(session) { attribute_updates: merge_all(updated_attr, data, add_attrs, expire_attr), return_values: 'UPDATED_NEW' } end
Attribute for creation of session.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 107 def created_attr { 'created_at' => updated_at } end
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 129 def data_attr(session) { 'data' => { value: session, action: 'PUT' } } end
Determine if data has been manipulated
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 134 def data_unchanged?(env, session) return false unless env['rack.initial_data'] env['rack.initial_data'] == session end
@return [Hash] Options for deleting session.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 47 def delete_opts(sid) table_opts(sid) end
Expected attributes
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 141 def expected_attributes(sid) { expected: { @config.table_key => { value: sid, exists: true } } } end
Update client with current time + max_stale.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 112 def expire_at max_stale = @config.max_stale || 0 { value: (Time.now + max_stale).to_i, action: 'PUT' } end
Attribute for TTL expiration of session.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 118 def expire_attr { 'expire_at' => expire_at } end
Each database operation is placed in this rescue wrapper. This wrapper will call the method, rescue any exceptions and then pass exceptions to the configured error handler.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 40 def handle_error(env = nil) yield rescue Aws::DynamoDB::Errors::ServiceError => e @config.error_handler.handle_error(e, env) end
@return [Hash] merged hash of all hashes passed in.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 152 def merge_all(*hashes) new_hash = {} hashes.each { |hash| new_hash.merge!(hash) } new_hash end
Marshal the data.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 75 def pack_data(data) [Marshal.dump(data)].pack('m*') end
@return [Hash] Options for saving an existing sesison in the database.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 67 def save_exists_opts(env, sid, session, options = {}) add_attr = options[:add_attrs] || {} expected = options[:expect_attr] || {} attribute_opts = merge_all(attr_updts(env, session, add_attr), expected) merge_all(table_opts(sid), attribute_opts) end
@return [Hash] Options for saving a new session in database.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 61 def save_new_opts(env, sid, session) attribute_opts = attr_updts(env, session, created_attr) merge_all(table_opts(sid), attribute_opts) end
Table
options for client.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 85 def table_opts(sid) { table_name: @config.table_name, key: { @config.table_key => sid } } end
Unmarshal the data.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 80 def unpack_data(packed_data) Marshal.load(packed_data.unpack1('m*')) end
@return [Hash] Options for updating item in Session table.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 52 def update_opts(env, sid, session, options = {}) if env['dynamo_db.new_session'] save_new_opts(env, sid, session) else save_exists_opts(env, sid, session, options) end end
Update client with current time attribute.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 102 def updated_at { value: Time.now.to_f.to_s, action: 'PUT' } end
Attribute for updating session.
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 123 def updated_attr { 'updated_at' => updated_at } end