| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # client::memcached Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Client::Memcached; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | servers => [ qw(server|server_list) ], | ||
| 20 | _c => [ qw(INTERNAL) ], | ||
| 21 | }, | ||
| 22 | attributes_default => { | ||
| 23 | servers => [ qw(127.0.0.1:11211) ], | ||
| 24 | }, | ||
| 25 | commands => { | ||
| 26 | open => [ qw(server|server_list|OPTIONAL) ], | ||
| 27 | close => [ ], | ||
| 28 | write => [ qw(key value) ], | ||
| 29 | read => [ qw(key) ], | ||
| 30 | stats => [ ], | ||
| 31 | get_slabs_info => [ ], | ||
| 32 | }, | ||
| 33 | require_modules => { | ||
| 34 | 'Cache::Memcached' => [ ], | ||
| 35 | }, | ||
| 36 | }; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub brik_init { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | $self->open or return 0; | ||
| 43 | |||
| 44 | return $self->SUPER::brik_init; | ||
| 45 | } | ||
| 46 | |||
| 47 | # | ||
| 48 | # run client::memcached open "[qw(127.0.0.1:11211)]" | ||
| 49 | # | ||
| 50 | sub open { | ||
| 51 | my $self = shift; | ||
| 52 | my ($servers) = @_; | ||
| 53 | |||
| 54 | $servers ||= $self->servers; | ||
| 55 | $self->brik_help_set_undef_arg('servers', $servers) or return; | ||
| 56 | |||
| 57 | my $c = Cache::Memcached->new({ | ||
| 58 | servers => $servers, | ||
| 59 | }); | ||
| 60 | if (!defined($c)) { | ||
| 61 | return $self->log->error("open: memcached failed: $!"); | ||
| 62 | } | ||
| 63 | $c->enable_compress(0); | ||
| 64 | |||
| 65 | return $self->_c($c); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub close { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my $c = $self->_c; | ||
| 72 | if (defined($c)) { | ||
| 73 | $c->disconnect_all; | ||
| 74 | $self->_c(undef); | ||
| 75 | } | ||
| 76 | |||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub write { | ||
| 81 | my $self = shift; | ||
| 82 | my ($k, $v) = @_; | ||
| 83 | |||
| 84 | my $c = $self->_c; | ||
| 85 | $self->brik_help_run_undef_arg('open', $c) or return; | ||
| 86 | $self->brik_help_run_undef_arg('write', $k) or return; | ||
| 87 | $self->brik_help_run_undef_arg('write', $v) or return; | ||
| 88 | |||
| 89 | return $c->set($k, $v); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub read { | ||
| 93 | my $self = shift; | ||
| 94 | my ($k) = @_; | ||
| 95 | |||
| 96 | my $c = $self->_c; | ||
| 97 | $self->brik_help_run_undef_arg('open', $c) or return; | ||
| 98 | $self->brik_help_run_undef_arg('read', $k) or return; | ||
| 99 | |||
| 100 | return $c->get($k); | ||
| 101 | } | ||
| 102 | |||
| 103 | # stats | ||
| 104 | # stats items | ||
| 105 | sub stats { | ||
| 106 | my $self = shift; | ||
| 107 | my ($k) = @_; | ||
| 108 | |||
| 109 | my $c = $self->_c; | ||
| 110 | $self->brik_help_run_undef_arg('open', $c) or return; | ||
| 111 | |||
| 112 | return $c->stats($k); | ||
| 113 | } | ||
| 114 | |||
| 115 | # | ||
| 116 | # High level functions: | ||
| 117 | # | ||
| 118 | sub get_slabs_info { | ||
| 119 | my $self = shift; | ||
| 120 | |||
| 121 | my $stats = $self->stats('items') or return; | ||
| 122 | |||
| 123 | my @keys = (); | ||
| 124 | for my $host (keys %{$stats->{hosts}}) { | ||
| 125 | my $items = $stats->{hosts}{$host}{items}; | ||
| 126 | my @lines = split(/\r\n/, $items); | ||
| 127 | |||
| 128 | # Example: 'STAT items:3:number 628916' | ||
| 129 | for my $line (@lines) { | ||
| 130 | my ($slab, $count) = $line =~ m{^STAT\s+items:(\d+):number\s+(\d+)}; | ||
| 131 | next unless (defined($slab) && defined($count)); | ||
| 132 | push @keys, [ $slab, $count ]; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | return \@keys; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ | ||