Module: RedisClient::Namespace::Middleware

Defined in:
lib/redis_client/namespace/middleware.rb

Overview

Middleware for RedisClient to add namespace support

This module implements the RedisClient middleware interface to intercept Redis commands and apply namespace transformations. It automatically prefixes keys with a namespace and removes the prefix from certain command results.

The middleware requires the following custom configuration:

  • namespace: The namespace prefix to apply (required)

  • separator: The separator between namespace and key (optional, default: “:”)

Examples:

Basic usage with RedisClient

client = RedisClient.config(
  middlewares: [RedisClient::Namespace::Middleware],
  custom: { namespace: "myapp", separator: ":" }
).new_client

client.call("SET", "key", "value")  # Actually sets "myapp:key"
client.call("GET", "key")           # Gets "myapp:key" and returns "value"

See Also:

Instance Method Summary collapse

Instance Method Details

#call(command, redis_config) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/redis_client/namespace/middleware.rb', line 28

def call(command, redis_config)
  namespace = redis_config.custom[:namespace] or return super
  separator = redis_config.custom[:separator] || ":"
  command = CommandBuilder.namespaced_command(command, namespace: namespace, separator: separator)
  super.tap do |result|
    CommandBuilder.trimed_result(command, result, namespace: namespace, separator: separator)
  end
end

#call_pipelined(commands, redis_config) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redis_client/namespace/middleware.rb', line 37

def call_pipelined(commands, redis_config)
  namespace = redis_config.custom[:namespace] or return super
  separator = redis_config.custom[:separator] || ":"
  commands = commands.map do |cmd|
    CommandBuilder.namespaced_command(cmd, namespace: namespace, separator: separator)
  end
  super.tap do |results|
    commands.each_with_index do |command, i|
      CommandBuilder.trimed_result(command, results[i], namespace: namespace, separator: separator)
    end
  end
end