1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
require 'uri'
module Moneta
module Adapters
class ActiveRecord
# @api private
class Backend
@connection_lock = ::Mutex.new
class << self
attr_reader :connection_lock
end
attr_reader :table_name
delegate :connection_handler, to: ::ActiveRecord::Base
def initialize(table:, connection: nil, **options)
@table_name = table
@connection = connection
if connection
@owner_name =
case connection
when Symbol, String
connection.to_s
when Hash
hash = connection.reject { |key| [:username, 'username', :password, 'password'].member?(key) }
'moneta?' + URI.encode_www_form(hash.to_a.sort)
when nil
nil
else
raise "Unexpected connection: #{connection}"
end
end
end
def connection_pool
if @connection
connection_handler.retrieve_connection_pool(@owner_name) ||
self.class.connection_lock.synchronize do
connection_handler.retrieve_connection_pool(@owner_name) ||
connection_handler.establish_connection(@connection, owner_name: @owner_name)
end
else
::ActiveRecord::Base.connection_pool
end
end
end
end
end
end
|