Class: Sqreen::Ecosystem::TransactionStorage

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/sqreen/ecosystem/transaction_storage.rb

Overview

The transaction storage is a mechanism for the modules to share request-scoped data with each other or to keep request-scoped objects themselves, although, for this last use case to be effective, propagation of request start/end events to the modules will likely be needed. A more generic notification of data availability to modules also may be needed in the future.

This is not be used to share data with the Ecosystem client

This class is not thread safe because it can call the lazy getter twice if [] is called concurrently.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransactionStorage

Returns a new instance of TransactionStorage.



40
41
42
43
# File 'lib/sqreen/ecosystem/transaction_storage.rb', line 40

def initialize
  @values = {}
  @values_lazy = {}
end

Class Method Details

.create_thread_localSqreen::Ecosystem::TransactionStorage



26
27
28
# File 'lib/sqreen/ecosystem/transaction_storage.rb', line 26

def create_thread_local
  Thread.current[:tx_storage] = new
end

.destroy_thread_localObject



35
36
37
# File 'lib/sqreen/ecosystem/transaction_storage.rb', line 35

def destroy_thread_local
  Thread.current[:tx_storage] = nil
end

.fetch_thread_localSqreen::Ecosystem::TransactionStorage



31
32
33
# File 'lib/sqreen/ecosystem/transaction_storage.rb', line 31

def fetch_thread_local
  Thread.current[:tx_storage]
end

Instance Method Details

#[](key) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sqreen/ecosystem/transaction_storage.rb', line 53

def [](key)
  v = @values[key]
  return v unless v.nil?

  v = @values_lazy[key]
  return nil if v.nil?

  begin
    @values[key] = v.call
  rescue StandardError => e
    logger.warn { "Error resolving key #{e} with lazy value: #{e.message}" }
    raise
  end
end

#[]=(key, value) ⇒ Object



45
46
47
# File 'lib/sqreen/ecosystem/transaction_storage.rb', line 45

def []=(key, value)
  @values[key] = value
end

#set_lazy(key, &block) ⇒ Object



49
50
51
# File 'lib/sqreen/ecosystem/transaction_storage.rb', line 49

def set_lazy(key, &block)
  @values_lazy[key] = block
end