Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The name parameter to new() can be a CodeRef

Usefull for more dynamic connection names.

Signed-off-by: Pedro Melo <melo@simplicidade.org>
  • Loading branch information...
commit ba6a9fdbde0a73f43ea11a9ed3bb17934db0608e 1 parent cbb5c9b
@melo melo authored
Showing with 43 additions and 10 deletions.
  1. +1 −0  Changes
  2. +27 −9 lib/Redis.pm
  3. +15 −1 t/42-client_cmds.t
View
1  Changes
@@ -1,6 +1,7 @@
Revision history for Redis
{{$NEXT}}
+ * Parameter name to new() can be a CodeRef, dynamic connection names
1.961 2013-01-23T15:09:47Z
* Add wait_one_response() for better pipeline management
View
36 lib/Redis.pm
@@ -55,7 +55,11 @@ sub new {
my $on_conn = $self->{on_connect};
$self->{on_connect} = sub {
my ($redis) = @_;
- try { $redis->client_setname($name) };
+ try {
+ my $n = $name;
+ $n = $n->($redis) if ref($n) eq 'CODE';
+ $redis->client_setname($n) if defined $n;
+ };
$on_conn->(@_) if $on_conn;
}
}
@@ -724,7 +728,15 @@ __END__
my $redis = Redis->new(server => 'redis.example.com:8080');
## Set the connection name (requires Redis 2.6.9)
- my $redis = Redis->new(server => 'redis.example.com:8080', name => 'my_connection_name');
+ my $redis = Redis->new(
+ server => 'redis.example.com:8080',
+ name => 'my_connection_name',
+ );
+ my $generation = 0;
+ my $redis = Redis->new(
+ server => 'redis.example.com:8080',
+ name => sub { "cache-$$-".++$generation },
+ );
## Use UNIX domain socket
my $redis = Redis->new(sock => '/path/to/socket');
@@ -883,7 +895,8 @@ utf-8 flag turned on.
my $r = Redis->new( reconnect => 60, every => 5000 );
my $r = Redis->new( password => 'boo' );
my $r = Redis->new( on_connect => sub { my ($redis) = @_; ... } );
- my $r = Redis->new( name => 'my_connection_name' ); ## Redis 2.6.9 required
+ my $r = Redis->new( name => 'my_connection_name' );
+ my $r = Redis->new( name => sub { "cache-for-$$" });
The C<< server >> parameter specifies the Redis server we should connect to,
via TCP. Use the 'IP:PORT' format. If no C<< server >> option is present, we
@@ -948,12 +961,17 @@ object.
You can also set a name for each connection. This can be very useful for
debugging purposes, using the C<< CLIENT LIST >> command. To set a connection
-name, use the C<< name >> parameter. Please note that there are restrictions on
-the name you can set, the most important of which is, no spaces. See the
-L<CLIENT SETNAME documentation|http://redis.io/commands/client-setname> for all
-the juicy details. This feature is safe to use with all versions of Redis
-servers. If C<< CLIENT SETNAME >> support is not available (Redis servers 2.6.9
-and above only), the name parameter is ignored.
+name, use the C<< name >> parameter. You can use both a scalar value or a
+CodeRef. If the latter, it will be called after each connection, with the Redis
+object, and it should return the connection name to use. If it returns a
+undefined value, Redis will not set the connection name.
+
+Please note that there are restrictions on the name you can set, the most
+important of which is, no spaces. See the L<CLIENT SETNAME
+documentation|http://redis.io/commands/client-setname> for all the juicy
+details. This feature is safe to use with all versions of Redis servers. If C<<
+CLIENT SETNAME >> support is not available (Redis servers 2.6.9 and above
+only), the name parameter is ignored.
The C<< debug >> parameter enables debug information to STDERR, including all
interactions with the server. You can also enable debug with the C<REDIS_DEBUG>
View
16 t/42-client_cmds.t
@@ -27,7 +27,21 @@ subtest 'client_{set|get}name commands' => sub {
subtest 'client name via constructor' => sub {
ok(my $r = Redis->new(server => $srv, name => 'buuu'), 'connected to our test redis-server, with a name');
- is($r->client_getname, 'buuu', '... name was properly set');
+ is($r->client_getname, 'buuu', '...... name was properly set');
+
+ ok($r = Redis->new(server => $srv, name => sub {"cache-for-$$"}), '... with a dynamic name');
+ is($r->client_getname, "cache-for-$$", '...... name was properly set');
+
+ ok($r = Redis->new(server => $srv, name => sub {undef}), '... with a dynamic name, but returning undef');
+ is($r->client_getname, undef, '...... name was not set');
+
+ my $generation = 0;
+ for (1 .. 3) {
+ ok($r = Redis->new(server => $srv, name => sub { "gen-$$-" . ++$generation }),
+ "Using dynamic name, for generation $generation");
+ my $n = "gen-$$-$generation";
+ is($r->client_getname, $n, "... name was set properly, '$n'");
+ }
};
Please sign in to comment.
Something went wrong with that request. Please try again.