Module: RedisCommandCounter

Defined in:
lib/redis_middleware.rb

Overview

RedisCommandCounter is RedisClient middleware.

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

Examples:

Enable Redis command counting

RedisCommandCounter.reset
RedisClient.register(RedisCommandCounter)

See Also:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.countInteger (readonly)

Gets the current count of Redis commands executed.

Returns:

  • (Integer)

    The number of Redis commands executed.



73
74
75
# File 'lib/redis_middleware.rb', line 73

def count
  @count
end

Class Method Details

.count_commandsObject



89
90
91
92
93
94
# File 'lib/redis_middleware.rb', line 89

def count_commands
  start_count = count
  yield
  end_count = count
  end_count - start_count
end

.incrementInteger

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

Returns:

  • (Integer)

    The new count after incrementing.



85
86
87
# File 'lib/redis_middleware.rb', line 85

def increment
  @mutex.synchronize { @count += 1 }
end

.resetInteger

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

Returns:

  • (Integer)

    The reset count (always 0).



78
79
80
# File 'lib/redis_middleware.rb', line 78

def reset
  @mutex.synchronize { @count = 0 }
end

Instance Method Details

#call(command, redis_config) ⇒ Object

Counts the Redis command and delegates its execution.

This method is called for each Redis command when the middleware is active. It increments the command count and then yields to execute the actual command.

Parameters:

  • command (Array)

    The Redis command and its arguments.

  • redis_config (Hash)

    The configuration options for the Redis connection.

Returns:

  • (Object)

    The result of the Redis command execution.



105
106
107
108
# File 'lib/redis_middleware.rb', line 105

def call(command, redis_config)
  RedisCommandCounter.increment
  yield
end