The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Redis - perl binding for Redis database

SYNOPSIS

    ## Defaults to $ENV{REDIS_SERVER} or 127.0.0.1:6379
    my $redis = Redis->new;
    
    my $redis = Redis->new(server => 'redis.example.com:8080');
    
    ## Use UNIX domain socket
    my $redis = Redis->new(sock => '/path/to/socket');
    
    ## Enable auto-reconnect
    ## Try to reconnect every 500ms up to 60 seconds until success
    ## Die if you can't after that
    my $redis = Redis->new(reconnect => 60);
    
    ## Try each 100ms upto 2 seconds (every is in milisecs)
    my $redis = Redis->new(reconnect => 2, every => 100);
    
    ## Disable the automatic utf8 encoding => much more performance
    my $redis = Redis->new(encoding => undef);
    
    ## Use all the regular Redis commands, they all accept a list of
    ## arguments
    ## See http://redis.io/commands for full list
    $redis->get('key');
    $redis->set('key' => 'value');
    $redis->sort('list', 'DESC');
    $redis->sort(qw{list LIMIT 0 5 ALPHA DESC});
    
    ## Publish/Subscribe
    $redis->subscribe(
      'topic_1',
      'topic_2',
      sub {
        my ($message, $topic, $subscribed_topic) = @_
    
          ## $subscribed_topic can be different from topic if
          ## you use psubscribe() with wildcards
      }
    );
    $redis->psubscribe('nasdaq.*', sub {...});
    
    ## Blocks and waits for messages, calls subscribe() callbacks
    ##  ... forever
    $redis->wait_for_messages($timeout) while 1;
    
    ##  ... until some condition
    $redis->wait_for_messages($timeout) while $keep_going;
    
    $redis->publish('topic_1', 'message');

DESCRIPTION

Pure perl bindings for http://redis.io/

This version supports protocol 2.x (multi-bulk) or later of Redis available at https://github.com/antirez/redis/.

This documentation lists commands which are exercised in test suite, but additional commands will work correctly since protocol specifies enough information to support almost all commands with same piece of code with a little help of AUTOLOAD.

METHODS

new

    my $r = Redis->new; # $ENV{REDIS_SERVER} or 127.0.0.1:6379

    my $r = Redis->new( server => '192.168.0.1:6379', debug => 0 );
    my $r = Redis->new( server => '192.168.0.1:6379', encoding => undef );
    my $r = Redis->new( sock => '/path/to/sock' );
    my $r = Redis->new( reconnect => 60, every => 5000 );

The server parameter specifies the Redis server we should connect to, via TCP. Use the 'IP:PORT' format. If no server option is present, we will attempt to use the REDIS_SERVER environment variable. If neither of those options are present, it defaults to '127.0.0.1:6379'.

Alternatively you can use the sock parameter to specify the path of the UNIX domain socket where the Redis server is listening.

The REDIS_SERVER can be used for UNIX domain sockets too. The following formats are supported:

/path/to/sock
unix:/path/to/sock
127.0.0.1:11011
tcp:127.0.0.1:11011

The encoding parameter speficies the encoding we will use to decode all the data we receive and encode all the data sent to the redis server. Due to backwards-compatibility we default to utf8. To disable all this encoding/decoding, you must use <encoding = undef>>. This is the recommended option.

Warning: this option has several problems and it is deprecated. A future version will add a safer option.

The reconnect option enables auto-reconnection mode. If we cannot connect to the Redis server, or if a network write fails, we enter retry mode. We will try a new connection every every miliseconds (1000ms by default), up-to reconnect seconds.

Be aware that read errors will always thrown an exception, and will not trigger a retry until the new command is sent.

If we cannot re-establish a connection after reconnect seconds, an exception will be thrown.

The debug parameter enables debug information to STDERR, including all interactions with the server. You can also enable debug with the REDIS_DEBUG environment variable.

Connection Handling

quit

  $r->quit;

ping

  $r->ping || die "no server?";

Commands operating on string values

set

  $r->set( foo => 'bar' );

  $r->setnx( foo => 42 );

get

  my $value = $r->get( 'foo' );

mget

  my @values = $r->mget( 'foo', 'bar', 'baz' );

incr

  $r->incr('counter');

  $r->incrby('tripplets', 3);

decr

  $r->decr('counter');

  $r->decrby('tripplets', 3);

exists

  $r->exists( 'key' ) && print "got key!";

del

  $r->del( 'key' ) || warn "key doesn't exist";

type

  $r->type( 'key' ); # = string

Commands operating on the key space

keys

  my @keys = $r->keys( '*glob_pattern*' );

randomkey

  my $key = $r->randomkey;

rename

  my $ok = $r->rename( 'old-key', 'new-key', $new );

dbsize

  my $nr_keys = $r->dbsize;

Commands operating on lists

See also Redis::List for tie interface.

rpush

  $r->rpush( $key, $value );

lpush

  $r->lpush( $key, $value );

llen

  $r->llen( $key );

lrange

  my @list = $r->lrange( $key, $start, $end );

ltrim

  my $ok = $r->ltrim( $key, $start, $end );

lindex

  $r->lindex( $key, $index );

lset

  $r->lset( $key, $index, $value );

lrem

  my $modified_count = $r->lrem( $key, $count, $value );

lpop

  my $value = $r->lpop( $key );

rpop

  my $value = $r->rpop( $key );

Commands operating on sets

sadd

  my $ok = $r->sadd( $key, $member );

scard

  my $n_elements = $r->scard( $key );

sdiff

  my @elements = $r->sdiff( $key1, $key2, ... );
  my $elements = $r->sdiff( $key1, $key2, ... ); # ARRAY ref

sdiffstore

  my $ok = $r->sdiffstore( $dstkey, $key1, $key2, ... );

sinter

  my @elements = $r->sinter( $key1, $key2, ... );
  my $elements = $r->sinter( $key1, $key2, ... ); # ARRAY ref

sinterstore

  my $ok = $r->sinterstore( $dstkey, $key1, $key2, ... );

sismember

  my $bool = $r->sismember( $key, $member );

smembers

  my @elements = $r->smembers( $key );
  my $elements = $r->smembers( $key ); # ARRAY ref

smove

  my $ok = $r->smove( $srckey, $dstkey, $element );

spop

  my $element = $r->spop( $key );

spop

  my $element = $r->srandmember( $key );

srem

  $r->srem( $key, $member );

sunion

  my @elements = $r->sunion( $key1, $key2, ... );
  my $elements = $r->sunion( $key1, $key2, ... ); # ARRAY ref

sunionstore

  my $ok = $r->sunionstore( $dstkey, $key1, $key2, ... );

Multiple databases handling commands

select

  $r->select( $dbindex ); # 0 for new clients

move

  $r->move( $key, $dbindex );

flushdb

  $r->flushdb;

flushall

  $r->flushall;

Sorting

sort

  $r->sort("key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA');

Persistence control commands

save

  $r->save;

bgsave

  $r->bgsave;

lastsave

  $r->lastsave;

shutdown

  $r->shutdown;

Remote server control commands

info

  my $info_hash = $r->info;

ENCODING

Since Redis knows nothing about encoding, we are forcing utf-8 flag on all data received from Redis. This change is introduced in 1.2001 version. Please note that this encoding option severely degrades performance

You can disable this automatic encoding by passing an option to new: encoding => undef.

This allows us to round-trip utf-8 encoded characters correctly, but might be problem if you push binary junk into Redis and expect to get it back without utf-8 flag turned on.

AUTHORS

Pedro Melo, <melo@cpan.org>

Original author and maintainer: Dobrica Pavlinusic, <dpavlin at rot13.org>

BUGS

Please report any bugs or feature requests to bug-redis at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Redis. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Redis
    perldoc Redis::List
    perldoc Redis::Hash

You can also look for information at:

ACKNOWLEDGEMENTS

The following persons contributed to this project (alphabetical order):

Dirk Vleugels
Flavio Poletti
Jeremy Zawodny
sunnavy at bestpractical.com
Thiago Berlitz Rondon
Ulrich Habel

COPYRIGHT & LICENSE

Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.

Copyright 2011-2012 Pedro Melo, all rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.