| 1 | package Model::Envoy::Storage::Redis; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.1.3'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use MooseX::ClassAttribute; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use JSON::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | extends 'Model::Envoy::Storage'; | |||||||||
| 10 | |||||||||||
| 11 | =head1 Model::Envoy::Storage::Redis | ||||||||||
| 12 | |||||||||||
| 13 | A storage plugin for C<Model::Envoy> that conforms to C<Model::Envoy::Storage>. | ||||||||||
| 14 | It does not implement C<list> at the moment, but will fetch, store and delete | ||||||||||
| 15 | objects out of the key-value store. | ||||||||||
| 16 | |||||||||||
| 17 | =cut | ||||||||||
| 18 | |||||||||||
| 19 | 1 | class_has 'redis' => ( | |||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | isa => 'Redis::Fast', | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | sub configure { | ||||||||
|
Subroutine
configure: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 25 | 1 | my ( $plugin_class, $envoy_class, $conf ) = @_; | |||||||||
| 26 | |||||||||||
| 27 | $plugin_class->redis( | ||||||||||
| 28 | ref $conf->{redis} eq 'CODE' ? $conf->{redis}->($envoy_class) : $conf->{redis} | ||||||||||
| 29 | 1 | ▶ | ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 1 | $conf->{_configured} = 1; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 3 | ▶ | sub fetch { | ||||||||
|
Subroutine
fetch: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 34 | 3 | my $self = shift; | |||||||||
| 35 | 3 | my $model_class = shift; | |||||||||
| 36 | |||||||||||
| 37 | 3 | my $id = do { | |||||||||
| 38 | |||||||||||
| 39 | 3 | ▶ | if ( @_ == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 2 | $_[0]; | |||||||||
| 41 | } | ||||||||||
| 42 | else { | ||||||||||
| 43 | 1 | my %params = @_; | |||||||||
| 44 | |||||||||||
| 45 | 1 | $params{id}; | |||||||||
| 46 | } | ||||||||||
| 47 | }; | ||||||||||
| 48 | |||||||||||
| 49 | 3 | ▶ | if ( my $result = $self->redis->get( 'id:' . $id ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 2 | return $model_class->build( decode_json( $result ) ); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 1 | return undef; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub save { | ||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 58 | 1 | my ( $self ) = @_; | |||||||||
| 59 | |||||||||||
| 60 | 1 | $self->redis->set( 'id:' . $self->model->id => encode_json( $self->model->dump ) ); | |||||||||
| 61 | |||||||||||
| 62 | 1 | return $self; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 1 | ▶ | sub list { | ||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 66 | |||||||||||
| 67 | 1 | return undef; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 1 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 71 | 1 | my ( $self ) = @_; | |||||||||
| 72 | |||||||||||
| 73 | 1 | $self->redis->del( 'id:' . $self->model->id ); | |||||||||
| 74 | |||||||||||
| 75 | 1 | return; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 3 | ▶ | sub in_storage { | ||||||||
|
Subroutine
in_storage: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 79 | 3 | my ( $self ) = @_; | |||||||||
| 80 | |||||||||||
| 81 | 3 | ▶ | return $self->redis->exists( 'id:' . $self->model->id ) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 1 | 1; | |||||||||