Module: DatabaseCommandCounter

Defined in:
lib/middleware/database_logger.rb

Overview

DatabaseCommandCounter is Valkey/RedisClient middleware.

This middleware counts the number of Database commands executed. It can be useful for performance monitoring and debugging, allowing you to track the volume of Database operations in your application.

Examples:

Enable Database command counting

DatabaseCommandCounter.reset
RedisClient.register(DatabaseCommandCounter)

See Also:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.skip_commandsUnsortedSet (readonly)

Gets the set of commands to skip counting.

Returns:

  • (UnsortedSet)

    The commands that won't be counted.



216
217
218
# File 'lib/middleware/database_logger.rb', line 216

def skip_commands
  @skip_commands
end

Class Method Details

.countInteger

Gets the current count of Database commands executed.

Returns:

  • (Integer)

    The number of Database commands executed.



220
221
222
# File 'lib/middleware/database_logger.rb', line 220

def count
  @count.value
end

.count_commands { ... } ⇒ Integer

Counts the number of Database commands executed within a block.

This method captures the command count before and after executing the provided block, returning the difference. This is useful for measuring how many Database commands are executed by a specific operation.

Examples:

Count commands in a block

commands_executed = DatabaseCommandCounter.count_commands do
  dbclient.set('key1', 'value1')
  dbclient.get('key1')
end
# commands_executed will be 2

Yields:

  • [] The block of code to execute while counting commands.

Returns:

  • (Integer)

    The number of Database commands executed within the block.



257
258
259
260
261
262
# File 'lib/middleware/database_logger.rb', line 257

def count_commands
  start_count = count      # Capture the current command count before execution
  yield                    # Execute the provided block
  end_count = count        # Capture the command count after execution
  end_count - start_count  # Return the difference (commands executed in block)
end

.incrementInteger

Increments the command count. This method is thread-safe.

Returns:

  • (Integer)

    The new count after incrementing.



234
235
236
# File 'lib/middleware/database_logger.rb', line 234

def increment
  @count.increment
end

.resetInteger

Resets the command count to zero. This method is thread-safe.

Returns:

  • (Integer)

    The reset count (always 0).



227
228
229
# File 'lib/middleware/database_logger.rb', line 227

def reset
  @count.value = 0
end

.skip_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/middleware/database_logger.rb', line 238

def skip_command?(command)
  skip_commands.include?(command.first.to_s.upcase)
end

Instance Method Details

#call(command, _config) ⇒ Object

Counts the Database command and delegates its execution.

This method is called for each Database command when the middleware is active. It increments the command count (unless the command is in the skip list) and then yields to execute the actual command.

Parameters:

  • command (Array)

    The Database command and its arguments.

  • _config (Hash)

    The configuration options for the Database connection.

Returns:

  • (Object)

    The result of the Database command execution.



278
279
280
281
# File 'lib/middleware/database_logger.rb', line 278

def call(command, _config)
  klass.increment unless klass.skip_command?(command)
  yield
end

#call_once(command, _config) ⇒ Object



291
292
293
294
# File 'lib/middleware/database_logger.rb', line 291

def call_once(command, _config)
  klass.increment unless klass.skip_command?(command)
  yield
end

#call_pipelined(commands, _config) ⇒ Object



283
284
285
286
287
288
289
# File 'lib/middleware/database_logger.rb', line 283

def call_pipelined(commands, _config)
  # Count all commands in the pipeline (except skipped ones)
  commands.each do |command|
    klass.increment unless klass.skip_command?(command)
  end
  yield
end

#klassObject



265
266
267
# File 'lib/middleware/database_logger.rb', line 265

def klass
  DatabaseCommandCounter
end