Module: DatabaseCommandCounter
- Defined in:
- lib/middleware/database_middleware.rb
Overview
DatabaseCommandCounter is 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.
rubocop:disable ThreadSafety/ClassInstanceVariable
Class Attribute Summary collapse
-
.skip_commands ⇒ Set
readonly
Gets the set of commands to skip counting.
Class Method Summary collapse
-
.count ⇒ Integer
Gets the current count of Database commands executed.
-
.count_commands { ... } ⇒ Integer
Counts the number of Database commands executed within a block.
-
.increment ⇒ Integer
Increments the command count.
-
.reset ⇒ Integer
Resets the command count to zero.
-
.skip_command?(command) ⇒ Boolean
Instance Method Summary collapse
-
#call(command, _config) ⇒ Object
Counts the Database command and delegates its execution.
-
#klass ⇒ Object
Class Attribute Details
.skip_commands ⇒ Set (readonly)
Gets the set of commands to skip counting.
83 84 85 |
# File 'lib/middleware/database_middleware.rb', line 83 def skip_commands @skip_commands end |
Class Method Details
.count ⇒ Integer
Gets the current count of Database commands executed.
87 88 89 |
# File 'lib/middleware/database_middleware.rb', line 87 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.
124 125 126 127 128 129 |
# File 'lib/middleware/database_middleware.rb', line 124 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 |
.increment ⇒ Integer
Increments the command count. This method is thread-safe.
101 102 103 |
# File 'lib/middleware/database_middleware.rb', line 101 def increment @count.increment end |
.reset ⇒ Integer
Resets the command count to zero. This method is thread-safe.
94 95 96 |
# File 'lib/middleware/database_middleware.rb', line 94 def reset @count.value = 0 end |
.skip_command?(command) ⇒ Boolean
105 106 107 |
# File 'lib/middleware/database_middleware.rb', line 105 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.
145 146 147 148 |
# File 'lib/middleware/database_middleware.rb', line 145 def call(command, _config) klass.increment unless klass.skip_command?(command) yield end |
#klass ⇒ Object
132 133 134 |
# File 'lib/middleware/database_middleware.rb', line 132 def klass DatabaseCommandCounter end |