| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # client::redis Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Client::Redis; | ||
| 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::System::Service Metabrik::System::Package); |
|
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 | server => [ qw(ip_address) ], | ||
| 20 | port => [ qw(port) ], | ||
| 21 | _redis => [ ], | ||
| 22 | }, | ||
| 23 | attributes_default => { | ||
| 24 | server => '127.0.0.1', | ||
| 25 | port => 6379, | ||
| 26 | }, | ||
| 27 | commands => { | ||
| 28 | install => [ ], # Inherited | ||
| 29 | start => [ ], # Inherited | ||
| 30 | stop => [ ], # Inherited | ||
| 31 | status => [ ], # Inherited | ||
| 32 | connect => [ ], | ||
| 33 | command => [ qw(command $arg1 $arg2 ... $argN) ], | ||
| 34 | command_as_list => [ qw(command $arg1 $arg2 ... $argN) ], | ||
| 35 | time => [ ], | ||
| 36 | disconnect => [ ], | ||
| 37 | quit => [ ], # Same as disconnect | ||
| 38 | dbsize => [ ], | ||
| 39 | exists => [ qw(key) ], | ||
| 40 | get => [ qw(key) ], | ||
| 41 | set => [ qw(key value) ], | ||
| 42 | del => [ qw(key) ], | ||
| 43 | mget => [ qw($key_list) ], | ||
| 44 | hset => [ qw(key $hash) ], | ||
| 45 | hget => [ qw(key hash_field) ], | ||
| 46 | hgetall => [ qw(key) ], | ||
| 47 | client_list => [ ], | ||
| 48 | client_getname => [ ], | ||
| 49 | list_databases => [ qw(database) ], | ||
| 50 | list_keys => [ qw(database keys|OPTIONAL) ], | ||
| 51 | flushall => [ ], | ||
| 52 | }, | ||
| 53 | require_modules => { | ||
| 54 | 'Redis' => [ ], | ||
| 55 | }, | ||
| 56 | need_packages => { | ||
| 57 | ubuntu => [ qw(redis-server) ], | ||
| 58 | debian => [ qw(redis-server) ], | ||
| 59 | kali => [ qw(redis-server) ], | ||
| 60 | }, | ||
| 61 | need_services => { | ||
| 62 | ubuntu => [ qw(redis-server) ], | ||
| 63 | debian => [ qw(redis-server) ], | ||
| 64 | }, | ||
| 65 | }; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub connect { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my $redis = Redis->new( | ||
| 72 | server => $self->server.':'.$self->port, | ||
| 73 | name => 'redis_connection', | ||
| 74 | cnx_timeout => defined($self->global) && $self->global->ctimeout || 3, | ||
| 75 | read_timeout => defined($self->global) && $self->global->rtimeout || 3, | ||
| 76 | write_timeout => defined($self->global) && $self->global->rtimeout || 3, | ||
| 77 | ) or return $self->log->error("connect: redis connection error"); | ||
| 78 | |||
| 79 | return $self->_redis($redis); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _get_redis { | ||
| 83 | my $self = shift; | ||
| 84 | |||
| 85 | my $redis = $self->_redis; | ||
| 86 | $self->brik_help_run_undef_arg('connect', $redis) or return; | ||
| 87 | |||
| 88 | return $redis; | ||
| 89 | } | ||
| 90 | |||
| 91 | # | ||
| 92 | # Command list: http://redis.io/commands | ||
| 93 | # | ||
| 94 | # Or 'run client::redis command command' | ||
| 95 | # | ||
| 96 | sub command { | ||
| 97 | my $self = shift; | ||
| 98 | my ($cmd, @args) = @_; | ||
| 99 | |||
| 100 | my $redis = $self->_get_redis or return; | ||
| 101 | |||
| 102 | my $r = $redis->$cmd(@args); | ||
| 103 | if (! defined($r)) { | ||
| 104 | return $self->log->error("command: $cmd failed"); | ||
| 105 | } | ||
| 106 | |||
| 107 | return $r; | ||
| 108 | } | ||
| 109 | |||
| 110 | # | ||
| 111 | # Dump content from db10: | ||
| 112 | # | ||
| 113 | # run client::redis command select 10 | ||
| 114 | # run client::redis command_as_list keys * | ||
| 115 | # | ||
| 116 | sub command_as_list { | ||
| 117 | my $self = shift; | ||
| 118 | my ($cmd, @args) = @_; | ||
| 119 | |||
| 120 | my $redis = $self->_get_redis or return; | ||
| 121 | |||
| 122 | my @r = $redis->$cmd(@args); | ||
| 123 | |||
| 124 | return \@r; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub time { | ||
| 128 | my $self = shift; | ||
| 129 | |||
| 130 | return $self->command('time'); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub disconnect { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | my $r = $self->command('quit') or return; | ||
| 137 | $self->_redis(undef); | ||
| 138 | |||
| 139 | return $r; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub quit { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | return $self->disconnect; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub dbsize { | ||
| 149 | my $self = shift; | ||
| 150 | |||
| 151 | return $self->command('dbsize'); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub exists { | ||
| 155 | my $self = shift; | ||
| 156 | my ($key) = @_; | ||
| 157 | |||
| 158 | $self->brik_help_run_undef_arg('exists', $key) or return; | ||
| 159 | |||
| 160 | return $self->command('exists', $key); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub get { | ||
| 164 | my $self = shift; | ||
| 165 | my ($key) = @_; | ||
| 166 | |||
| 167 | $self->brik_help_run_undef_arg('get', $key) or return; | ||
| 168 | |||
| 169 | return $self->command('get', $key); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub set { | ||
| 173 | my $self = shift; | ||
| 174 | my ($key, $value) = @_; | ||
| 175 | |||
| 176 | $self->brik_help_run_undef_arg('set', $key) or return; | ||
| 177 | $self->brik_help_run_undef_arg('set', $value) or return; | ||
| 178 | |||
| 179 | return $self->command('set', $key, $value); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub del { | ||
| 183 | my $self = shift; | ||
| 184 | my ($key) = @_; | ||
| 185 | |||
| 186 | $self->brik_help_run_undef_arg('del', $key) or return; | ||
| 187 | |||
| 188 | return $self->command('del', $key); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub mget { | ||
| 192 | my $self = shift; | ||
| 193 | my ($key_list) = @_; | ||
| 194 | |||
| 195 | $self->brik_help_run_undef_arg('mget', $key_list) or return; | ||
| 196 | $self->brik_help_run_invalid_arg('mget', $key_list, 'ARRAY') or return; | ||
| 197 | |||
| 198 | return $self->command('mget', @$key_list); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub hset { | ||
| 202 | my $self = shift; | ||
| 203 | my ($hashname, $hash) = @_; | ||
| 204 | |||
| 205 | $self->brik_help_run_undef_arg('hset', $hashname) or return; | ||
| 206 | $self->brik_help_run_undef_arg('hset', $hash) or return; | ||
| 207 | $self->brik_help_run_invalid_arg('hset', $hash, 'HASH') or return; | ||
| 208 | |||
| 209 | my $redis = $self->_get_redis or return; | ||
| 210 | |||
| 211 | for (keys %$hash) { | ||
| 212 | $redis->hset($hashname, $_, $hash->{$_}) or next; | ||
| 213 | } | ||
| 214 | |||
| 215 | $redis->wait_all_responses; | ||
| 216 | |||
| 217 | return $hash; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub hget { | ||
| 221 | my $self = shift; | ||
| 222 | my ($hashname, $field) = @_; | ||
| 223 | |||
| 224 | $self->brik_help_run_undef_arg('hget', $hashname) or return; | ||
| 225 | $self->brik_help_run_undef_arg('hget', $field) or return; | ||
| 226 | |||
| 227 | return $self->command('hget', $hashname, $field); | ||
| 228 | } | ||
| 229 | |||
| 230 | sub hgetall { | ||
| 231 | my $self = shift; | ||
| 232 | my ($hashname) = @_; | ||
| 233 | |||
| 234 | $self->brik_help_run_undef_arg('hgetall', $hashname) or return; | ||
| 235 | |||
| 236 | my $r = $self->command('hgetall', $hashname) or return; | ||
| 237 | |||
| 238 | my %h = @{$r}; | ||
| 239 | |||
| 240 | return \%h; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub client_list { | ||
| 244 | my $self = shift; | ||
| 245 | |||
| 246 | my $r = $self->command('client_list') or return; | ||
| 247 | |||
| 248 | return [ split(/\n/, $r) ]; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub client_getname { | ||
| 252 | my $self = shift; | ||
| 253 | |||
| 254 | my $r = $self->command('client_getname') or return; | ||
| 255 | |||
| 256 | return [ split(/\n/, $r) ]; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub list_databases { | ||
| 260 | my $self = shift; | ||
| 261 | |||
| 262 | return $self->command('info', 'keyspace'); | ||
| 263 | } | ||
| 264 | |||
| 265 | sub list_keys { | ||
| 266 | my $self = shift; | ||
| 267 | my ($database, $keys) = @_; | ||
| 268 | |||
| 269 | $keys ||= '*'; | ||
| 270 | $self->brik_help_run_undef_arg('list_keys', $database) or return; | ||
| 271 | |||
| 272 | my $r = $self->command('select', $database) or return; | ||
| 273 | $self->log->info("list_keys: $r"); | ||
| 274 | |||
| 275 | my @r = $self->command_as_list('keys', $keys) or return; | ||
| 276 | |||
| 277 | return \@r; | ||
| 278 | } | ||
| 279 | |||
| 280 | sub flushall { | ||
| 281 | my $self = shift; | ||
| 282 | |||
| 283 | return $self->command('flushall'); | ||
| 284 | } | ||
| 285 | |||
| 286 | 1; | ||
| 287 | |||
| 288 | __END__ | ||