Async-Redis
view release on metacpan or search on metacpan
# Or with dependencies
cpanm Future::IO Future::AsyncAwait Protocol::Redis IO::Async
# Optional for better performance:
cpanm Protocol::Redis::XS
```
## Quick Start
```perl
use Async::Redis;
use Future::AsyncAwait;
# Future::IO 0.23+ has a built-in poll-based impl that works out of the box.
# For IO::Async or UV, require the impl directly:
# require Future::IO::Impl::IOAsync;
# require Future::IO::Impl::UV;
my $redis = Async::Redis->new(
host => 'localhost',
port => 6379,
);
(async sub {
await $redis->connect;
# Basic commands
await $redis->set('foo', 'bar');
my $value = await $redis->get('foo');
print "Value: $value\n";
# Pipelining
my $pipe = $redis->pipeline;
$pipe->set('k1', 'v1');
$pipe->set('k2', 'v2');
$pipe->get('k1');
my $results = await $pipe->execute;
$redis->disconnect;
})->()->await;
```
## Usage Examples
### Connection Options
```perl
my $redis = Async::Redis->new(
# Basic connection
host => 'localhost',
port => 6379,
# Or use URI
uri => 'redis://user:pass@host:6379/1',
# Authentication
password => 'secret',
username => 'myuser', # Redis 6+ ACL
database => 1,
# Timeouts
connect_timeout => 10,
request_timeout => 5,
# Auto-reconnect
reconnect => 1,
reconnect_delay => 0.1,
reconnect_delay_max => 60,
# TLS
tls => {
ca_file => '/path/to/ca.crt',
cert_file => '/path/to/client.crt',
key_file => '/path/to/client.key',
},
# Key prefix (applied to all commands)
prefix => 'myapp:',
);
```
### Pipelining
```perl
my $pipe = $redis->pipeline;
$pipe->set('key1', 'value1');
$pipe->set('key2', 'value2');
$pipe->incr('counter');
$pipe->get('key1');
my $results = await $pipe->execute;
# $results = ['OK', 'OK', 1, 'value1']
```
### PubSub
```perl
# Subscriber
my $sub = await $redis->subscribe('news', 'alerts');
while (my $msg = await $sub->next_message) {
print "Channel: $msg->{channel}, Message: $msg->{message}\n";
}
# Publisher (on different connection)
await $redis->publish('news', 'Breaking news!');
```
### Transactions
```perl
my $results = await $redis->multi(async sub {
my ($tx) = @_;
$tx->set('key', 'value');
$tx->incr('counter');
});
# $results = ['OK', 1]
# With WATCH for optimistic locking
my $results = await $redis->watch_multi(['counter'], async sub {
my ($tx, $values) = @_;
my $current = $values->{counter} // 0;
( run in 0.562 second using v1.01-cache-2.11-cpan-39bf76dae61 )