Changeset 558

Show
Ignore:
Timestamp:
05/14/07 17:32:53 (2 years ago)
Author:
hachi
Message:

Add hook support to memcache client, and define a few hooks for use in gathering speed statistics.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/api/perl/ChangeLog

    r538 r558  
     1        * Add hook support, and define a few hooks for use in gathering 
     2          statistics. 
     3 
    14        * let parser_class be configured as a constructor option, 
    25          defaulting to XS if available, else regular.  (unless 
  • trunk/api/perl/lib/Cache/Memcached.pm

    r556 r558  
    2525    bucketcount _single_sock _stime 
    2626    connect_timeout cb_connect_fail 
    27     parser_class 
     27    parser_class hooks 
    2828}; 
    2929 
     
    8383    $self->{namespace} = $args->{namespace} || ''; 
    8484    $self->{namespace_len} = length $self->{namespace}; 
     85    $self->{hooks} = {}; 
    8586 
    8687    return $self; 
     
    412413    return 0 unless $sock; 
    413414 
     415    $self->run_hook('delete_start', $self); 
     416 
    414417    $self->{'stats'}->{"delete"}++; 
    415418    $key = ref $key ? $key->[1] : $key; 
     
    423426    } 
    424427 
     428    $self->run_hook('delete_end', $self); 
     429 
    425430    return $res eq "DELETED\r\n"; 
    426431} 
     
    447452    return 0 unless $sock; 
    448453 
     454    $self->run_hook('set_start', $self); 
     455 
    449456    use bytes; # return bytes from length() 
    450457 
     
    491498        $self->{'stat_callback'}->($stime, $etime, $sock, $cmdname); 
    492499    } 
     500 
     501    $self->run_hook('set_end', $self); 
    493502 
    494503    return $res eq "STORED\r\n"; 
     
    543552    $self->{'stats'}->{"get_multi"}++; 
    544553 
     554    $self->run_hook('get_start', $self); 
     555 
    545556    my %val;        # what we'll be returning a reference to (realkey -> value) 
    546557    my %sock_keys;  # sockref_as_scalar -> [ realkeys ] 
     
    549560    if ($self->{'_single_sock'}) { 
    550561        $sock = $self->sock_to_host($self->{'_single_sock'}); 
    551         return {} unless $sock; 
     562        unless ($sock) { 
     563            $self->run_hook('get_start', $self); 
     564            return {}; 
     565        } 
    552566        foreach my $key (@_) { 
    553567            my $kval = ref $key ? $key->[1] : $key; 
     
    582596 
    583597    _load_multi($self, \%sock_keys, \%val); 
     598 
     599    $self->run_hook('get_end', $self); 
    584600 
    585601    if ($self->{'debug'}) { 
     
    904920} 
    905921 
    906  
     922sub run_hook { 
     923    my Cache::Memcached $self = shift; 
     924    my $hookname = shift || return; 
     925 
     926    my $hook = $self->{hooks}->{$hookname}; 
     927    return unless $hook; 
     928 
     929    eval { $hook->(@_) }; 
     930 
     931    warn "Cache::Memcached hook '$hookname' threw error: $@\n" if $@; 
     932
     933 
     934sub add_hook { 
     935    my Cache::Memcached $self = shift; 
     936    my $hookname = shift || return; 
     937 
     938    if (@_) { 
     939        $self->{hooks}->{$hookname} = shift; 
     940    } else { 
     941        delete $self->{hooks}->{$hookname}; 
     942    } 
     943
    907944 
    9089451;