Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Redis::Streams;
- use 5.20.1;
- use constant ID_DELIM => ':::';
- use cmnstant DFLT_REDIS_DBN => 4;
- use enum qw[REDC CLIENT_ID STREAMS];
- use Redis::Fast;
- use FindBin;
- use Sys::Hostname qw(hostname);
- sub new {
- my ($class, %opts) = @_;
- my $client_id = delete($opts{'client_id'}) // join(ID_DELIM() => __hostname(), $FindBin::RealScript);
- my $redis_dbn = delete($opts{'redis_dbn'}) // DFLT_REDIS_DBN;
- my $redc = Redis::Fast;
- $redc->select($redis_dbn) if $redis_dbn;
- bless [$redc, $client_id, +{}], ref($class) || $class
- }
- sub __not_empty_string {
- ! ref($_[0]) and defined($_[0]) and length($_[0])
- }
- sub __hostname {
- state $hostname = hostname;
- $hostname
- }
- sub default_stream {
- my $self = $_[0];
- defined($_[1])
- ? do {
- __not_empty_string($_[1]) or die 'incorrect stream name';
- $self->[DEFAULT_STREAM] = $_[1]
- }
- : $self->[DEFAULT_STREAM]
- }
- sub tail_stream {
- my $self = $_[0];
- my $stream =
- $#_ > 0
- ? (__not_empty_string($_[1]) || die('incorrect stream name'), $stream)
- : $self->default_stream;
- my $start_id = ( $self->[STREAMS]{$stream} //= $self->[REDC]->get(join ID_DELIM() => $self->[CLIENT_ID], $stream) // '-' );
- if ( my @msgs = map @{$_}, $self->xrange($stream, $start_id, '+') ) {
- }
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement