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');

## 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 additinal commands will work correctly since protocol specifies enough information to support almost all commands with same peace of code with a little help of C <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 );

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

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

srem

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

scard

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

sismember

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

sinter

$r->sinter( $key1, $key2, ... );

sinterstore

my $ok = $r->sinterstore( $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
Jeremy Zawodny
sunnavy at bestpractical.com

COPYRIGHT & LICENSE

Copyright 2009-2010 Dobrica Pavlinusic, all rights reserved.

Copyright 2011 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.