Sha256: d894be93cdc2a839c97fe3bd88f9f7f91fb3e44366712a1fb26e4e34ec81c60d
Contents?: true
Size: 984 Bytes
Versions: 9
Compression:
Stored size: 984 Bytes
Contents
# frozen_string_literal: true module MiniSql module Abstract class PreparedCache DEFAULT_MAX_SIZE = 500 def initialize(connection, max_size = nil) @connection = connection @max_size = max_size || DEFAULT_MAX_SIZE @cache = {} @counter = 0 end 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 def next_key "s#{@counter += 1}" end def alloc(_) raise NotImplementedError, "must be implemented by specific database driver" end def dealloc(_) raise NotImplementedError, "must be implemented by specific database driver" end end end end
Version data entries
9 entries across 9 versions & 1 rubygems