NAME

Mojo::Redis::Cursor - Iterate the results from SCAN, SSCAN, HSCAN and ZSCAN

SYNOPSIS

use Mojo::Redis;
my $redis  = Mojo::Redis->new;
my $cursor = $redis->cursor(hkeys => 'redis:scan_test:hash');
my $keys   = $cursor->all;

DESCRIPTION

Mojo::Redis::Cursor provides methods for iterating over the result from the Redis commands SCAN, SSCAN, HSCAN and ZSCAN.

See https://redis.io/commands/scan for more information.

ATTRIBUTES

command

$array_ref = $cursor->command;

The current command used to get data from Redis. This need to be set in the constructor, but reading it out might not reflect the value put in. Examples:

$r->new(command => [hgetall => "foo*"]);
# $r->command == [hscan => "foo*", 0]

$r->new(command => [SSCAN => "foo*"])
# $r->command == [SSCAN => "foo*", 0]

Also, calling "next" will change the value of "command". Example:

$r->new(command => ["keys"]);
# $r->command == [scan => 0]
$r->next;
# $r->command == [scan => 42]

connection

$conn   = $cursor->connection;
$cursor = $cursor->connection(Mojo::Redis::Connection->new);

Holds a Mojo::Redis::Connection object.

finished

$bool = $cursor->finished;

True after calling "all" or if "next" has iterated the whole list of members.

redis

$conn   = $cursor->connection;
$cursor = $cursor->connection(Mojo::Redis::Connection->new);

Holds a Mojo::Redis object used to create the connections to talk with Redis.

METHODS

again

$cursor->again;

Used to reset the cursor and make "next" start over.

all

$res    = $cursor->all;
$cursor = $cursor->all(sub { my ($cursor, $res) = @_ });

Used to return all members. $res is an array ref of strings, except when using the command "hgetall".

all_p

$promise = $cursor->all_p->then(sub { my $res = shift });

Same as "all" but returns a Mojo::Promise.

new

$cursor = Mojo::Redis::Cursor->new(command => [...], redis => Mojo::Redis->new);

Used to construct a new object. "command" and "redis" is required as input.

Here are some examples of the differnet commands that are supported:

# Custom cursor commands
$cursor = $cursor->cursor(hscan => 0, match => '*', count => 100);
$cursor = $cursor->cursor(scan  => 0, match => '*', count => 100);
$cursor = $cursor->cursor(sscan => 0, match => '*', count => 100);
$cursor = $cursor->cursor(zscan => 0, match => '*', count => 100);

# Convenient cursor commands
$cursor = $cursor->cursor(hgetall  => "some:hash:key");
$cursor = $cursor->cursor(hkeys    => "some:hash:key");
$cursor = $cursor->cursor(keys     => "some:key:pattern*");
$cursor = $cursor->cursor(smembers => "some:set:key");

The convenient commands are alternatives to "hgetall" in Mojo::Redis::Database, "hkeys" in Mojo::Redis::Database, "keys" in Mojo::Redis::Database and "smembers" in Mojo::Redis::Database.

next

$res    = $cursor->next;
$cursor = $cursor->next(sub { my ($cursor, $err, $res) = @_ });

Used to return a chunk of members. $res is an array ref of strings, except when using the command "hgetall". $res will also be undef() when the cursor is exhausted and "finished" will be true.

next_p

$promise = $cursor->next_p;

Same as "next" but returns a Mojo::Prmoise.

SEE ALSO

Mojo::Redis.