class ActiveSupport::Cache::DynamoStore

Constants

CONTENT_KEY
DEFAULT_HASH_KEY
DEFAULT_TTL_KEY

Attributes

data[R]
dynamodb_client[R]
hash_key[R]
table_name[R]
ttl_key[R]

Public Class Methods

new( table_name:, dynamo_client: nil, hash_key: DEFAULT_HASH_KEY, ttl_key: DEFAULT_TTL_KEY, **opts ) click to toggle source

Instantiate the store.

Example:

ActiveSupport::Cache::Dynamo.new(table_name: 'CacheTable')
  => hash_key: 'CacheKey', ttl_key: 'TTL', table_name: 'CacheTable'

ActiveSupport::Cache::Dynamo.new(
  table_name: 'CacheTable',
  dynamo_client: client,
  hash_key: 'name',
  ttl_key: 'key_ttl'
)
Calls superclass method
# File lib/active_support/cache/dynamo_store.rb, line 34
def initialize(
  table_name:,
  dynamo_client: nil,
  hash_key: DEFAULT_HASH_KEY,
  ttl_key: DEFAULT_TTL_KEY,
  **opts
)
  super(opts)
  @table_name      = table_name
  @dynamodb_client = dynamo_client || Aws::DynamoDB::Client.new
  @ttl_key         = ttl_key
  @hash_key        = hash_key
end

Protected Instance Methods

delete_entry(name, _options) click to toggle source
# File lib/active_support/cache/dynamo_store.rb, line 76
def delete_entry(name, _options)
  dynamodb_client.delete_item(
    key: { hash_key => name },
    table_name: table_name
  )
end
read_entry(name, _options = nil) click to toggle source
# File lib/active_support/cache/dynamo_store.rb, line 50
def read_entry(name, _options = nil)
  result = dynamodb_client.get_item(
    key: { hash_key => name },
    table_name: table_name
  )

  return if result.item.nil? || result.item[CONTENT_KEY].nil?

  Marshal.load(result.item[CONTENT_KEY]) # rubocop:disable Security/MarshalLoad
rescue TypeError
  nil
end
write_entry(name, value, _options = nil) click to toggle source
# File lib/active_support/cache/dynamo_store.rb, line 63
def write_entry(name, value, _options = nil)
  item = {
    hash_key => name,
    CONTENT_KEY => StringIO.new(Marshal.dump(value))
  }

  item[ttl_key] = value.expires_at.to_i if value.expires_at

  dynamodb_client.put_item(item: item, table_name: table_name)

  true
end