class MiniSql::Abstract::PreparedCache

Constants

DEFAULT_MAX_SIZE

Public Class Methods

new(connection, max_size = nil) click to toggle source
# File lib/mini_sql/abstract/prepared_cache.rb, line 9
def initialize(connection, max_size = nil)
  @connection = connection
  @max_size = max_size || DEFAULT_MAX_SIZE
  @cache = {}
  @counter = 0
end

Public Instance Methods

prepare_statement(sql) click to toggle source
# File lib/mini_sql/abstract/prepared_cache.rb, line 16
def prepare_statement(sql)
  stm_key = "#{@connection.object_id}-#{sql}"
  statement = @cache.delete(stm_key)
  if statement
    @cache[stm_key] = statement
  else
    statement = @cache[stm_key] = alloc(sql)
    dealloc(@cache.shift.last) if @cache.length > @max_size
  end

  statement
end

Private Instance Methods

alloc(_) click to toggle source
# File lib/mini_sql/abstract/prepared_cache.rb, line 35
def alloc(_)
  raise NotImplementedError, "must be implemented by specific database driver"
end
dealloc(_) click to toggle source
# File lib/mini_sql/abstract/prepared_cache.rb, line 39
def dealloc(_)
  raise NotImplementedError, "must be implemented by specific database driver"
end
next_key() click to toggle source
# File lib/mini_sql/abstract/prepared_cache.rb, line 31
def next_key
  "s#{@counter += 1}"
end