module Aws::SessionStore::DynamoDB::GarbageCollection

Collects and deletes unwanted sessions based on their creation and update dates.

Public Instance Methods

batch_delete(config, items) click to toggle source

Deletes the batch gotten from the scan result. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 55
def batch_delete(config, items)
  begin
    subset = items.shift(25)
    sub_batch = write(subset)
    process!(config, sub_batch)
  end until subset.empty?
end
collect_garbage(options = {}) click to toggle source

Scans DynamoDB session table to find sessions that match the max age and max stale period requirements. it then deletes all of the found sessions.

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 12
def collect_garbage(options = {})
  config = load_config(options)
  last_key = eliminate_unwanted_sessions(config)
  while !last_key.empty?
    last_key = eliminate_unwanted_sessions(config, last_key)
  end
end
eliminate_unwanted_sessions(config, last_key = nil) click to toggle source

Scans and deletes batch. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 38
def eliminate_unwanted_sessions(config, last_key = nil)
  scan_result = scan(config, last_key)
  batch_delete(config, scan_result[:items])
  scan_result[:last_evaluated_key] || {}
end
load_config(options = {}) click to toggle source

Loads configuration options. @option (see Configuration#initialize) @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 23
def load_config(options = {})
  Aws::SessionStore::DynamoDB::Configuration.new(options)
end
oldest_date(sec) click to toggle source

@return [Hash] Hash with specified date attributes. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 102
def oldest_date(sec)
  hash = {}
  hash[:attribute_value_list] = [:n => "#{((Time.now - sec).to_f)}"]
  hash[:comparison_operator] = 'LT'
  hash
end
process!(config, sub_batch) click to toggle source

Proccesses pending request items. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 75
def process!(config, sub_batch)
  return if sub_batch.empty?
  opts = {}
  opts[:request_items] = {config.table_name => sub_batch}
  begin
    response = config.dynamo_db_client.batch_write_item(opts)
    opts[:request_items] = response[:unprocessed_items]
  end until opts[:request_items].empty?
end
scan(config, last_item = nil) click to toggle source

Scans the table for sessions matching the max age and max stale time specified. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 47
def scan(config, last_item = nil)
  options = scan_opts(config)
  options = options.merge(start_key(last_item)) if last_item
  config.dynamo_db_client.scan(options)
end
scan_filter(config) click to toggle source

Sets scan filter attributes based on attributes specified. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 29
def scan_filter(config)
  hash = {}
  hash['created_at'] = oldest_date(config.max_age) if config.max_age
  hash['updated_at'] = oldest_date(config.max_stale) if config.max_stale
  { :scan_filter => hash }
end
scan_opts(config) click to toggle source

Provides scan options. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 87
def scan_opts(config)
  table_opts(config).merge(scan_filter(config))
end
start_key(last_item) click to toggle source

Provides start key. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 111
def start_key(last_item)
  { :exclusive_start_key => last_item }
end
table_opts(config) click to toggle source

Provides table options @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 93
def table_opts(config)
  {
    :table_name => config.table_name,
    :attributes_to_get => [config.table_key]
  }
end
write(sub_batch) click to toggle source

Turns array into correct format to be passed in to a delete request. @api private

# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 66
def write(sub_batch)
  sub_batch.inject([]) do |rqst_array, item|
    rqst_array << {:delete_request => {:key => item}}
    rqst_array
  end
end