« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/DSL.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Cache/Memcached/Fast.pm »
| 1 | package Dwarf::Plugin::AnyEvent::Redis; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use Dwarf::Util qw/add_method/; | ||
| 4 | use AnyEvent::Redis; | ||
| 5 | |||
| 6 | sub init { | ||
| 7 | my ($class, $c, $opt) = @_; | ||
| 8 | $opt ||= {}; | ||
| 9 | $opt->{default_connection} ||= 'master'; | ||
| 10 | $opt->{on_error} ||= sub { warn @_ }; | ||
| 11 | $opt->{on_cleanup} ||= sub { warn "Connection closed: @_" }; | ||
| 12 | |||
| 13 | add_method($c, redis => sub { | ||
| 14 | my ($self, $key) = @_; | ||
| 15 | $key ||= $opt->{default_connection}; | ||
| 16 | |||
| 17 | my $conf = $self->conf('redis') | ||
| 18 | or return; | ||
| 19 | |||
| 20 | $self->{'dwarf.redis'} ||= do { | ||
| 21 | my $repo; | ||
| 22 | for my $key (keys %{ $conf }) { | ||
| 23 | $repo->{$key} = AnyEvent::Redis->new({ | ||
| 24 | host => $conf->{$key}->{host}, | ||
| 25 | port => $conf->{$key}->{port}, | ||
| 26 | encoding => $conf->{$key}->{encoding} || 'utf8', | ||
| 27 | }); | ||
| 28 | $repo->{$key}->select($conf->{$key}->{dbid} // 0); | ||
| 29 | } | ||
| 30 | $repo; | ||
| 31 | }; | ||
| 32 | |||
| 33 | my $redis = $self->{'dwarf.redis'}; | ||
| 34 | return $redis->{$key} if exists $redis->{$key}; | ||
| 35 | return; | ||
| 36 | }); | ||
| 37 | } | ||
| 38 | |||
| 39 | 1; |