Algorithm-FloodControl

 view release on metacpan or  search on metacpan

lib/Algorithm/FloodControl/Backend/Cache/Memcached.pm  view on Meta::CPAN

package Algorithm::FloodControl::Backend::Cache::Memcached;

use utf8;
use strict;
use warnings;

use Params::Validate qw/:all/;
use base 'Algorithm::FloodControl::Backend';

=head2 increment

=cut

sub increment {
    my ( $self )       = @_;
    my $is_added   = $self->storage->add( $self->_tail_name, 0 );    # If it does not exists
    my $last_value = 0;
    if ( ! $is_added ) {
        $last_value = $self->storage->incr( $self->_tail_name );
    }
    $self->storage->set( $self->_item_name($last_value) => time + $self->expires, $self->expires );
    return $last_value;
}

=head2 clear

=cut

sub clear {
    my ($self) = @_;
    return $self->storage->delete( $self->_tail_name );
}

=head2 get_item

=cut

sub get_item {
    my $self = shift;
    my $item = shift;
    return $self->storage->get( $self->_item_name($item) );
}

sub _last_number {
    my $self = shift;
    return $self->storage->get( $self->_tail_name );
}

sub _tail_name {
    my $self = shift;
    return $self->prefix . "_end";
}

sub _item_name {
    my $self = shift;
    my $item = shift;
    return $self->prefix . "_$item";
}

1;


__END__



( run in 0.579 second using v1.01-cache-2.11-cpan-f0fbb3f571b )