| 1 | package Myriad::Service::Storage; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use Myriad::Class; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | our $VERSION = '1.001'; # VERSION | ||||||||||||
| 6 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||||||||||||
| 7 | |||||||||||||
| 8 | =encoding utf8 | ||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Myriad::Service::Storage - microservice storage abstraction layer | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||
| 15 | |||||||||||||
| 16 | my $storage = $myriad->storage; | ||||||||||||
| 17 | await $storage->get('some_key'); | ||||||||||||
| 18 | await $storage->hash_add('some_key', 'hash_key', 13); | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||
| 21 | |||||||||||||
| 22 | This module provides service storage access. | ||||||||||||
| 23 | |||||||||||||
| 24 | It implements L<Myriad::Role::Storage> in an object available as the C<$storage> | ||||||||||||
| 25 | lexical in any service class. See that module for more details on the API. | ||||||||||||
| 26 | |||||||||||||
| 27 | =cut | ||||||||||||
| 28 | |||||||||||||
| 29 | 1 | ▶ | use Myriad::Role::Storage; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | 1 | ▶ | use Metrics::Any qw($metrics); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | |||||||||||||
| 32 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=10.5
| |||||||||||||
| 33 | 1 | $metrics->make_timer(time_elapsed => | |||||||||||
| 34 | name => [qw(myriad storage)], | ||||||||||||
| 35 | description => 'Time taken to process storage request', | ||||||||||||
| 36 | labels => [qw(method status service)], | ||||||||||||
| 37 | ); | ||||||||||||
| 38 | |||||||||||||
| 39 | 1 | my $meta = Object::Pad::MOP::Class->for_class('Myriad::Service::Storage'); | |||||||||||
| 40 | 1 | for my $method (@Myriad::Role::Storage::WRITE_METHODS, @Myriad::Role::Storage::READ_METHODS) { | |||||||||||
| 41 | 0 | ▶ | $meta->add_method($method, sub { | ||||||||||
|
Subroutine
hash_as_list: not called CC=1 SCAR=6.9
| |||||||||||||
| 42 | 0 | my ($self, $key, @rest) = @_; | |||||||||||
| 43 | 0 | ▶ | return $self->storage->$method($self->apply_prefix($key), @rest)->on_ready(sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 44 | 0 | my $f = shift; | |||||||||||
| 45 | 0 | ▶ | $metrics->report_timer(time_elapsed => $f->elapsed // 0, {method => $method, status => $f->state, service => $self->prefix}); | ||||||||||
|
Condition0%0/2
$f->elapsed // 0
MC/DC0%0/1
$f->elapsed // 0
$f->elapsed
Truth table0%0/2
A
A= $f->elapsed
| |||||||||||||
| 46 | 0 | }); | |||||||||||
| 47 | 23 | }); | |||||||||||
| 48 | } | ||||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | has $storage; | ||||||||||||
| 52 | has $prefix; | ||||||||||||
| 53 | |||||||||||||
| 54 | method storage { $storage } | ||||||||||||
| 55 | method prefix { $prefix } | ||||||||||||
| 56 | |||||||||||||
| 57 | BUILD (%args) { | ||||||||||||
| 58 | my $service_prefix = delete $args{prefix} // die 'need a prefix'; | ||||||||||||
| 59 | $prefix = "service.$service_prefix"; | ||||||||||||
| 60 | $storage = delete $args{storage} // die 'need a storage instance'; | ||||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | =head2 apply_prefix | ||||||||||||
| 64 | |||||||||||||
| 65 | Maps the requested key into the service's keyspace | ||||||||||||
| 66 | so we can pass it over to the generic storage layer. | ||||||||||||
| 67 | |||||||||||||
| 68 | Takes the following parameters: | ||||||||||||
| 69 | |||||||||||||
| 70 | =over 4 | ||||||||||||
| 71 | |||||||||||||
| 72 | =item * C<$k> - the key | ||||||||||||
| 73 | |||||||||||||
| 74 | =back | ||||||||||||
| 75 | |||||||||||||
| 76 | Returns the modified key. | ||||||||||||
| 77 | |||||||||||||
| 78 | =cut | ||||||||||||
| 79 | |||||||||||||
| 80 | method apply_prefix ($k) { | ||||||||||||
| 81 | return $prefix . '/' . $k; | ||||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 1; | ||||||||||||
| 85 | |||||||||||||
| 86 | =head1 AUTHOR | ||||||||||||
| 87 | |||||||||||||
| 88 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||||||||||||
| 89 | |||||||||||||
| 90 | See L<Myriad/CONTRIBUTORS> for full details. | ||||||||||||
| 91 | |||||||||||||
| 92 | =head1 LICENSE | ||||||||||||
| 93 | |||||||||||||
| 94 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||||||||||||
| 95 | |||||||||||||