line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Memcached::CLI::Item; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
6
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
46
|
|
4
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
32
|
|
5
|
2
|
|
|
2
|
|
25
|
use 5.008_001; |
|
2
|
|
|
|
|
17
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
6
|
use POSIX 'strftime'; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
11
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
99
|
use App::Memcached::CLI::Util ':all'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
221
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
8
|
use version; our $VERSION = 'v0.7.1'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
6
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my @FIELDS = qw(key value length expire flags cas); |
14
|
|
|
|
|
|
|
my %DISP_METHOD_OF = ( |
15
|
|
|
|
|
|
|
value => 'disp_value', |
16
|
|
|
|
|
|
|
length => 'disp_length', |
17
|
|
|
|
|
|
|
expire => 'disp_expire', |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $DISPLAY_DATA_LENGTH = 320; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
24
|
0
|
|
|
|
|
|
my %data = @_; |
25
|
0
|
|
|
|
|
|
bless \%data, $class; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub find { |
29
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
30
|
0
|
|
|
|
|
|
my $keys = shift; |
31
|
0
|
|
|
|
|
|
my $ds = shift; |
32
|
0
|
|
|
|
|
|
my %opt = @_; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
0
|
|
|
|
my $command = $opt{command} || 'get'; |
35
|
0
|
|
|
|
|
|
my $list = $ds->$command($keys); |
36
|
0
|
|
|
|
|
|
my @items; |
37
|
0
|
|
|
|
|
|
for my $data (@$list) { |
38
|
0
|
|
|
|
|
|
push(@items, bless($data, $class)); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
return \@items; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub save { |
45
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
46
|
0
|
|
|
|
|
|
my $ds = shift; |
47
|
0
|
|
|
|
|
|
my %opt = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
for my $key (qw/flags expire value/) { |
50
|
0
|
0
|
|
|
|
|
if ($opt{$key}) { $self->{$key} = $opt{$key}; } |
|
0
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
my %option = ( |
53
|
|
|
|
|
|
|
flags => $self->{flags}, |
54
|
|
|
|
|
|
|
expire => $self->{expire}, |
55
|
0
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
0
|
|
|
|
my $command = $opt{command} || 'set'; |
58
|
0
|
0
|
|
|
|
|
if ($command eq 'cas') { |
59
|
0
|
|
|
|
|
|
return $ds->$command(@$self{qw/key value cas/}, %option); |
60
|
|
|
|
|
|
|
} else { |
61
|
0
|
|
|
|
|
|
return $ds->$command(@$self{qw/key value/}, %option); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub remove { |
66
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
67
|
0
|
|
|
|
|
|
my $ds = shift; |
68
|
0
|
|
|
|
|
|
my $ret = $ds->delete($self->{key}); |
69
|
0
|
|
|
|
|
|
return $ret; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub output { |
73
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
74
|
0
|
|
|
|
|
|
my $space = q{ } x 4; |
75
|
0
|
|
|
|
|
|
for my $key (@FIELDS) { |
76
|
0
|
|
|
|
|
|
my $value = $self->{$key}; |
77
|
0
|
0
|
|
|
|
|
if (my $_method = $DISP_METHOD_OF{$key}) { |
78
|
0
|
|
|
|
|
|
$value = $self->$_method; |
79
|
|
|
|
|
|
|
} |
80
|
0
|
0
|
|
|
|
|
next unless defined $value; |
81
|
0
|
|
|
|
|
|
printf "%s%6s:%s%s\n", $space, $key, $space, $value; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub output_line { |
86
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
87
|
0
|
|
|
|
|
|
my @kv; |
88
|
0
|
|
|
|
|
|
for my $key (@FIELDS) { |
89
|
0
|
|
|
|
|
|
my $value = $self->{$key}; |
90
|
0
|
0
|
|
|
|
|
if (my $_method = $DISP_METHOD_OF{$key}) { |
91
|
0
|
|
|
|
|
|
$value = $self->$_method; |
92
|
|
|
|
|
|
|
} |
93
|
0
|
0
|
|
|
|
|
next unless defined $value; |
94
|
0
|
|
|
|
|
|
push @kv, join(q{:}, $key, $value); |
95
|
|
|
|
|
|
|
} |
96
|
0
|
|
|
|
|
|
printf "%s\n", join("\t", @kv); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub disp_length { |
100
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
101
|
|
|
|
|
|
|
$self->{disp_length} ||= sub { |
102
|
0
|
0
|
|
0
|
|
|
return unless (defined $self->{length}); |
103
|
0
|
|
|
|
|
|
my $length = $self->{length}; |
104
|
0
|
0
|
|
|
|
|
if ($length >= 1024) { |
105
|
0
|
|
|
|
|
|
return sprintf '%.1fKB', $length / 1024.0; |
106
|
|
|
|
|
|
|
} |
107
|
0
|
|
|
|
|
|
return "${length}B"; |
108
|
0
|
|
0
|
|
|
|
}->(); |
109
|
0
|
|
|
|
|
|
return $self->{disp_length}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub disp_expire { |
113
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
114
|
|
|
|
|
|
|
$self->{disp_expire} ||= sub { |
115
|
0
|
0
|
|
0
|
|
|
return unless (defined $self->{expire}); |
116
|
0
|
|
|
|
|
|
return strftime('%F %T', localtime($self->{expire})); |
117
|
0
|
|
0
|
|
|
|
}->(); |
118
|
0
|
|
|
|
|
|
return $self->{disp_expire}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub disp_value { |
122
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
123
|
|
|
|
|
|
|
$self->{disp_value} ||= sub { |
124
|
0
|
|
|
0
|
|
|
my $text = $self->value_text; |
125
|
0
|
0
|
|
|
|
|
return unless (defined $text); |
126
|
0
|
0
|
|
|
|
|
return $text if (length $text <= $DISPLAY_DATA_LENGTH); |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my $length = length $text; |
129
|
0
|
|
|
|
|
|
my $result = substr($text, 0, $DISPLAY_DATA_LENGTH - 1); |
130
|
0
|
|
|
|
|
|
$result .= '...(the rest is skipped)'; |
131
|
0
|
|
|
|
|
|
return $result; |
132
|
0
|
|
0
|
|
|
|
}->(); |
133
|
0
|
|
|
|
|
|
return $self->{disp_value}; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub value_text { |
137
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
138
|
|
|
|
|
|
|
$self->{value_text} ||= sub { |
139
|
0
|
0
|
|
0
|
|
|
return unless (defined $self->{value}); |
140
|
0
|
0
|
|
|
|
|
if ($self->{value} !~ m/^[\x21-\x7e\s]/) { |
141
|
0
|
|
|
|
|
|
return '(Not ASCII)'; |
142
|
|
|
|
|
|
|
} |
143
|
0
|
|
|
|
|
|
return $self->{value}; |
144
|
0
|
|
0
|
|
|
|
}->(); |
145
|
0
|
|
|
|
|
|
return $self->{value_text}; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
__END__ |