module Aws::SessionStore::DynamoDB::GarbageCollection
Collects and deletes unwanted sessions based on their creation and update dates.
Public Class Methods
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. @option (see Configuration#initialize)
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 13 def collect_garbage(options = {}) config = load_config(options) last_key = eliminate_unwanted_sessions(config) last_key = eliminate_unwanted_sessions(config, last_key) until last_key.empty? end
Private Class Methods
batch_delete(config, items)
click to toggle source
Deletes the batch gotten from the scan result.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 51 def batch_delete(config, items) loop do subset = items.shift(25) sub_batch = write(subset) process!(config, sub_batch) break if subset.empty? end end
eliminate_unwanted_sessions(config, last_key = nil)
click to toggle source
Scans and deletes batch.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 36 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)
# 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
Provides specified date attributes.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 94 def oldest_date(sec) { attribute_value_list: [n: (Time.now - sec).to_f.to_s], comparison_operator: 'LT' } end
process!(config, sub_batch)
click to toggle source
Processes pending request items.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 69 def process!(config, sub_batch) return if sub_batch.empty? opts = { request_items: { config.table_name => sub_batch } } loop do response = config.dynamo_db_client.batch_write_item(opts) opts[:request_items] = response[:unprocessed_items] break if opts[:request_items].empty? end end
scan(config, last_item = nil)
click to toggle source
Scans the table for sessions matching the max age and max stale time specified.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 44 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.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 28 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.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 81 def scan_opts(config) table_opts(config).merge(scan_filter(config)) end
start_key(last_item)
click to toggle source
Provides start key.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 102 def start_key(last_item) { exclusive_start_key: last_item } end
table_opts(config)
click to toggle source
Provides table options
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 86 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.
# File lib/aws/session_store/dynamo_db/garbage_collection.rb, line 62 def write(sub_batch) sub_batch.each_with_object([]) do |item, rqst_array| rqst_array << { delete_request: { key: item } } end end