Advertisement
DRVTiny

redis_sream_tail.pl

Feb 18th, 2020
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.36 KB | None | 0 0
  1. package Redis::Streams;
  2. use 5.20.1;
  3.  
  4. use constant ID_DELIM => ':::';
  5. use cmnstant DFLT_REDIS_DBN => 4;
  6. use enum qw[REDC CLIENT_ID STREAMS];
  7. use Redis::Fast;
  8. use FindBin;
  9. use Sys::Hostname qw(hostname);
  10.  
  11. sub new {
  12.     my ($class, %opts) = @_;
  13.     my $client_id = delete($opts{'client_id'}) // join(ID_DELIM() => __hostname(), $FindBin::RealScript);
  14.     my $redis_dbn = delete($opts{'redis_dbn'}) // DFLT_REDIS_DBN;
  15.     my $redc = Redis::Fast;
  16.     $redc->select($redis_dbn) if $redis_dbn;
  17.     bless [$redc, $client_id, +{}], ref($class) || $class
  18. }
  19.  
  20. sub __not_empty_string {
  21.     ! ref($_[0]) and defined($_[0]) and length($_[0])
  22. }
  23.  
  24. sub __hostname {
  25.     state $hostname = hostname;
  26.     $hostname
  27. }
  28.  
  29. sub default_stream {
  30.     my $self = $_[0];
  31.     defined($_[1])
  32.         ? do {
  33.             __not_empty_string($_[1]) or die 'incorrect stream name';
  34.             $self->[DEFAULT_STREAM] = $_[1]
  35.           }
  36.         : $self->[DEFAULT_STREAM]
  37. }
  38.  
  39. sub tail_stream {
  40.     my $self = $_[0];
  41.     my  $stream =
  42.     $#_ > 0
  43.         ? (__not_empty_string($_[1]) || die('incorrect stream name'), $stream)
  44.         : $self->default_stream;
  45.     my $start_id = ( $self->[STREAMS]{$stream} //= $self->[REDC]->get(join ID_DELIM() => $self->[CLIENT_ID], $stream) // '-' );
  46.     if ( my @msgs = map @{$_}, $self->xrange($stream, $start_id, '+') ) {
  47.        
  48.     }
  49. }
  50.  
  51. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement