BusyBird

 view release on metacpan or  search on metacpan

lib/BusyBird/StatusStorage/Memory.pm  view on Meta::CPAN

            @_ = (undef, []);
            goto $args{callback};
        }
        $start_index = $tl_index;
    }
    my @indice = grep {
        if(!$ack_test->($self->{timelines}{$timeline}[$_])) {
            0;
        }elsif(defined($start_index) && $_ < $start_index) {
            0;
        }else {
            1;
        }
    } 0 .. $#{$self->{timelines}{$timeline}};
    $count = int(@indice) if $count eq 'all';
    $count = min($count, int(@indice));
    my $result_statuses = $count <= 0 ? [] : [ map {
        dclone($self->{timelines}{$timeline}[$_])
    } @indice[0 .. ($count-1)] ];

    @_ = (undef, $result_statuses);
    goto $args{callback};
}

1;

__END__

=pod

=head1 NAME

BusyBird::StatusStorage::Memory - Simple status storage in the process memory

=head1 SYNOPSIS

    use BusyBird::StatusStorage::Memory;
    
    ## The statuses are stored in the process memory.
    my $storage = BusyBird::StatusStorage::Memory->new();

    ## Load statuses from a file
    $storage->load("my_statuses.json");
    
    ## Save the content of the storage into a file
    $storage->save("my_statuses.json");


=head1 DESCRIPTION

This module is an implementation of L<BusyBird::StatusStorage>.

This storage stores all statuses in the process memory.
The stored statuses can be saved to a file in JSON format.
The saved statuses can be loaded from the file.

This storage is rather for testing purposes.
If you want a light-weight in-memory status storage,
I recommend L<BusyBird::StatusStorage::SQLite>.

This storage is synchronous, i.e., all operations block the thread
and the callback is called before the method returns.

This module uses L<BusyBird::Log> for logging.

=head1 CAVEATS

=over

=item *

Because this storage stores statuses in the process memory,
forked servers cannot share the storage.

=item *

Because this storage stores statuses in the process memory,
the stored statuses are lost when the process is terminated.

=back

=head1 CLASS METHODS

=head2 $storage = BusyBird::StatusStorage::Memory->new(%options)

Creates the storage object.

You can specify the folowing options in C<%options>.

=over

=item C<max_status_num> => MAX_STATUS_NUM (optional, default: 2000)

Specifies the maximum number of statuses the storage can store per timeline.
If more statuses are added to a full timeline, the oldest statuses in the timeline are removed automatically.

=back

=head1 OBJECTS METHODS

In addition to the following methods,
all methods described in L<BusyBird::StatusStorage> are supported, too.


=head2 $is_success = $storage->save($filepath)

Saves the current content of the storage to the file named C<$filepath>.

In success, it returns true. In failure, it returns false and the error will be logged.


=head2 $is_success = $storage->load($filepath)

Loads statuses from the file named C<$filepath>.

In success, it returns true. In failure, it returns false and the error will be logged.


=head1 AUTHOR

Toshio Ito C<< <toshioito [at] cpan.org> >>



( run in 1.367 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )