BusyBird
view release on metacpan or search on metacpan
lib/BusyBird/StatusStorage/Memory.pm view on Meta::CPAN
package BusyBird::StatusStorage::Memory;
use v5.8.0;
use strict;
use warnings;
use parent ('BusyBird::StatusStorage');
use BusyBird::Util qw(set_param sort_statuses);
use BusyBird::Log qw(bblog);
use BusyBird::StatusStorage::Common qw(contains ack_statuses get_unacked_counts);
use BusyBird::DateTime::Format;
use Storable qw(dclone);
use Carp;
use List::Util qw(min);
use JSON;
use Try::Tiny;
sub new {
my ($class, %options) = @_;
my $self = bless {
timelines => {}, ## timelines should always be sorted.
}, $class;
$self->set_param(\%options, 'max_status_num', 2000);
if($self->{max_status_num} <= 0) {
croak "max_status_num option must be bigger than 0.";
}
return $self;
}
sub _log {
my ($self, $level, $msg) = @_;
bblog($level, $msg);
}
sub _index {
my ($self, $timeline, $id) = @_;
return -1 if not defined($self->{timelines}{$timeline});
my $tl = $self->{timelines}{$timeline};
my @ret = grep { $tl->[$_]{id} eq $id } 0..$#$tl;
confess "multiple IDs in timeline $timeline." if int(@ret) >= 2;
return int(@ret) == 0 ? -1 : $ret[0];
}
sub _acked {
my ($self, $status) = @_;
no autovivification;
return $status->{busybird}{acked_at};
}
sub save {
my ($self, $filepath) = @_;
if(not defined($filepath)) {
croak '$filepath is not specified.';
}
my $file;
if(!open $file, ">", $filepath) {
$self->_log("error", "Cannot open $filepath to write.");
return 0;
}
my $success;
try {
print $file encode_json($self->{timelines});
$success = 1;
}catch {
my $e = shift;
$self->_log("error", "Error while saving: $e");
$success = 0;
};
close $file;
return $success;
}
sub load {
my ($self, $filepath) = @_;
if(not defined($filepath)) {
croak '$filepath is not specified.';
( run in 0.682 second using v1.01-cache-2.11-cpan-39bf76dae61 )