AnyEvent-KVStore

 view release on metacpan or  search on metacpan

lib/AnyEvent/KVStore.pm  view on Meta::CPAN


my $kvs_module = Type::Tiny->new(
    name       => 'Module',
    constraint => sub { $_->does('AnyEvent::KVStore::Driver')},
    message    => sub { "Not a kvstore driver object: $_"},
);

has _proxy => ( is => 'lazy', isa => $kvs_module, builder => \&_connect,
                handles => 'AnyEvent::KVStore::Driver');

sub _connect($){
    my ($self) = @_;
    local $@ = undef;
    my $modname = "AnyEvent::KVStore::" . ucfirst($self->module);
    eval "require $modname" or die $@;
    return $modname->new($self->config);
}

has module => (is => => 'ro', isa => Str, required => 1);

=head2 config

lib/AnyEvent/KVStore/Hash.pm  view on Meta::CPAN


=head2 watch

In this module, watches are run synchronously, not via AnyEvent's event loop.

If you wish to use AnyEvent's event loop, use condition variables with
callbacks set and C<send> them.

=cut

sub read($$) {
    my ($self, $key) = @_;
    return $self->_store->{$key};
}

sub exists($$) {
    my ($self, $key) = @_;
    my $href = $self->_store;
    return exists $href->{$key};
}

sub list($$) {
    my ($self, $prefix) = @_;
    return grep { $_ =~ /^$prefix/ } keys %{$self->_store}
        if defined $prefix and $prefix ne '';
    return keys %{$self->_store};
}

sub write($$$) {
    my ($self, $key, $value) = @_;
    # check watches
    if (exists $self->_watches->{''}){
        for my $w (@{$self->_watches->{''}}){
            $w->{cb}($key, $value);
        }
    }
    my $first_letter = substr($key, 0, 1);
    if (exists $self->_watches->{$first_letter}){
        for my $w (@{$self->_watches->{$first_letter}}){

lib/AnyEvent/KVStore/Hash.pm  view on Meta::CPAN

        }
    }
    if (not defined $value) {
        delete $self->_store->{$key};
        return 1;
    }
    $self->_store->{$key} = $value;
    return 1;
}

sub watch($$&) {
    my ($self, $pfx, $cb) = @_;
    my $first;
    if ($pfx eq '' or not defined $pfx){
       $first = '';
    } else {
       $first = substr($pfx, 0, 1);
    }
    $self->_watches->{$first} //= [];
    push @{$self->_watches->{$first}}, {pfx => $pfx, cb => $cb};
    return 1;



( run in 0.507 second using v1.01-cache-2.11-cpan-65fba6d93b7 )