| 1 | package IO::Iron::IronCache::Client; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | ## no critic (Documentation::RequirePodAtEnd) | ||||||||||||||
| 4 | ## no critic (Documentation::RequirePodSections) | ||||||||||||||
| 5 | ## no critic (Subroutines::RequireArgUnpacking) | ||||||||||||||
| 6 | |||||||||||||||
| 7 | 6 | ▶ | use 5.010_000; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | # Global creator | ||||||||||||||
| 12 | BEGIN { | ||||||||||||||
| 13 | 6 | ▶ | use parent qw(IO::Iron::ClientBase); # Inheritance | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 6 | ▶ | use parent qw(IO::Iron::IronCache::Policy ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | # Global destructor | ||||||||||||||
| 18 | ▶ | END { | |||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | |||||||||||||||
| 22 | # ABSTRACT: IronCache (Online Item-Value Storage) Client. | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 6 | our $VERSION = '0.13'; # VERSION: generated by DZP::OurPkgVersion | |||||||||||||
| 25 | |||||||||||||||
| 26 | |||||||||||||||
| 27 | |||||||||||||||
| 28 | 6 | ▶ | use Log::Any qw{$log}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 6 | ▶ | use Hash::Util 0.06 qw{lock_keys lock_keys_plus unlock_keys legal_keys}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 6 | ▶ | use Carp::Assert::More; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 6 | ▶ | use English '-no_match_vars'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 6 | ▶ | use Params::Validate qw(:all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 6 | ▶ | use IO::Iron::IronCache::Api (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 6 | ▶ | use IO::Iron::Common (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 6 | require IO::Iron::Connection; | |||||||||||||
| 37 | 6 | require IO::Iron::IronCache::Cache; | |||||||||||||
| 38 | |||||||||||||||
| 39 | # CONSTANTS for this package | ||||||||||||||
| 40 | |||||||||||||||
| 41 | # DEFAULTS | ||||||||||||||
| 42 | 6 | ▶ | use Const::Fast; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | |||||||||||||||
| 44 | # Service specific! | ||||||||||||||
| 45 | 6 | const my $DEFAULT_API_VERSION => '1'; | |||||||||||||
| 46 | 6 | const my $DEFAULT_HOST => 'cache-aws-us-east-1.iron.io'; | |||||||||||||
| 47 | |||||||||||||||
| 48 | |||||||||||||||
| 49 | |||||||||||||||
| 50 | |||||||||||||||
| 51 | |||||||||||||||
| 52 | sub new { | ||||||||||||||
| 53 | 2 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 54 | my %params = validate( | ||||||||||||||
| 55 | @_, { | ||||||||||||||
| 56 | 2 | map { $_ => { type => SCALAR, optional => 1 }, } IO::Iron::Common::IRON_CLIENT_PARAMETERS(), ## no critic (ValuesAndExpressions::ProhibitCommaSeparatedStatements) | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | ); | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 2 | $log->tracef('Entering new(%s, %s)', $class, \%params); | |||||||||||||
| 61 | 2 | my $self = IO::Iron::ClientBase->new(); | |||||||||||||
| 62 | # Add more keys to the self hash. | ||||||||||||||
| 63 | my @self_keys = ( | ||||||||||||||
| 64 | 'caches', # References to all objects created of class IO::Iron::IronCache::Cache. Not in use! | ||||||||||||||
| 65 | 'policy', # The policies of this client. | ||||||||||||||
| 66 | 2 | legal_keys(%{$self}), | |||||||||||||
| 67 | ); | ||||||||||||||
| 68 | 2 | unlock_keys(%{$self}); | |||||||||||||
| 69 | 2 | lock_keys_plus(%{$self}, @self_keys); | |||||||||||||
| 70 | 2 | my $config = IO::Iron::Common::get_config(%params); | |||||||||||||
| 71 | 2 | $log->debugf('The config: %s', $config); | |||||||||||||
| 72 | 2 | ▶ | $self->{'project_id'} = defined $config->{'project_id'} ? $config->{'project_id'} : undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 2 | $self->{'caches'} = []; | |||||||||||||
| 74 | 2 | assert_nonblank( $self->{'project_id'}, 'self->{project_id} is not defined or is blank'); | |||||||||||||
| 75 | |||||||||||||||
| 76 | 2 | unlock_keys(%{$self}); | |||||||||||||
| 77 | 2 | bless $self, $class; | |||||||||||||
| 78 | 2 | lock_keys(%{$self}, @self_keys); | |||||||||||||
| 79 | |||||||||||||||
| 80 | # Set up the policies | ||||||||||||||
| 81 | # We have to do this after blessing the object | ||||||||||||||
| 82 | # because get_policies has late bindings. | ||||||||||||||
| 83 | 2 | $self->{'policy'} = $self->get_policies('policies' => $config->{'policies'}); | |||||||||||||
| 84 | |||||||||||||||
| 85 | # Set up the connection client | ||||||||||||||
| 86 | my $connection = IO::Iron::Connection->new( { | ||||||||||||||
| 87 | 'project_id' => $config->{'project_id'}, | ||||||||||||||
| 88 | 'token' => $config->{'token'}, | ||||||||||||||
| 89 | 'host' => defined $config->{'host'} ? $config->{'host'} : $DEFAULT_HOST, | ||||||||||||||
| 90 | 'protocol' => $config->{'protocol'}, | ||||||||||||||
| 91 | 'port' => $config->{'port'}, | ||||||||||||||
| 92 | 'api_version' => defined $config->{'api_version'} ? $config->{'api_version'} : $DEFAULT_API_VERSION, | ||||||||||||||
| 93 | 'timeout' => $config->{'timeout'}, | ||||||||||||||
| 94 | 2 | ▶ | 'connector' => $params{'connector'}, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 95 | } | ||||||||||||||
| 96 | ); | ||||||||||||||
| 97 | 2 | $self->{'connection'} = $connection; | |||||||||||||
| 98 | 2 | $log->debugf('IronCache client created with config: (project_id=%s; token=%s; host=%s; timeout=%s).', $config->{'project_id'}, $config->{'token'}, $config->{'host'}, $config->{'timeout'}); | |||||||||||||
| 99 | 2 | $log->tracef('Exiting new: %s', $self); | |||||||||||||
| 100 | 2 | return $self; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | |||||||||||||||
| 104 | sub get_caches { | ||||||||||||||
| 105 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
get_caches: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 106 | 0 | validate( | |||||||||||||
| 107 | @_, { | ||||||||||||||
| 108 | # No parameters | ||||||||||||||
| 109 | } | ||||||||||||||
| 110 | ); | ||||||||||||||
| 111 | 0 | $log->tracef('Entering get_caches()'); | |||||||||||||
| 112 | |||||||||||||||
| 113 | 0 | my @caches; | |||||||||||||
| 114 | 0 | my $connection = $self->{'connection'}; | |||||||||||||
| 115 | 0 | my ($http_status_code, $response_message) = $connection->perform_iron_action( | |||||||||||||
| 116 | IO::Iron::IronCache::Api::IRONCACHE_LIST_CACHES(), { } ); | ||||||||||||||
| 117 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||||||
| 118 | 0 | foreach my $cache_info (@{$response_message}) { | |||||||||||||
| 119 | 0 | my $get_cache_name = $cache_info->{'name'}; | |||||||||||||
| 120 | my $cache = IO::Iron::IronCache::Cache->new({ | ||||||||||||||
| 121 | 'ironcache_client' => $self, # Pass a reference to the parent object. | ||||||||||||||
| 122 | 'name' => $get_cache_name, | ||||||||||||||
| 123 | 'connection' => $self->{'connection'}, | ||||||||||||||
| 124 | 0 | 'policy' => $self->{'policy'}, | |||||||||||||
| 125 | }); | ||||||||||||||
| 126 | 0 | push @caches, $cache; | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | #push @{$self->{'caches'}}, @caches; # Store only created caches! | ||||||||||||||
| 129 | 0 | $log->debugf('Created caches: %s', \@caches); | |||||||||||||
| 130 | |||||||||||||||
| 131 | 0 | $log->tracef('Exiting get_caches: %s', \@caches); | |||||||||||||
| 132 | 0 | return @caches; | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | |||||||||||||||
| 136 | sub get_info_about_cache { | ||||||||||||||
| 137 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
get_info_about_cache: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 138 | 0 | my %params = validate( | |||||||||||||
| 139 | @_, { | ||||||||||||||
| 140 | 'name' => { type => SCALAR, }, # cache name. | ||||||||||||||
| 141 | } | ||||||||||||||
| 142 | ); | ||||||||||||||
| 143 | 0 | $log->tracef('Entering get_info_about_cache(%s)', \%params); | |||||||||||||
| 144 | |||||||||||||||
| 145 | 0 | my $connection = $self->{'connection'}; | |||||||||||||
| 146 | my ($http_status_code, $response_message) = $connection->perform_iron_action( | ||||||||||||||
| 147 | IO::Iron::IronCache::Api::IRONCACHE_GET_INFO_ABOUT_A_CACHE(), | ||||||||||||||
| 148 | 0 | { '{Cache Name}' => $params{'name'}, } | |||||||||||||
| 149 | ); | ||||||||||||||
| 150 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||||||
| 151 | 0 | my $info = $response_message; | |||||||||||||
| 152 | |||||||||||||||
| 153 | # info: | ||||||||||||||
| 154 | # {'id':'523566104a734c39bf00041e','project_id':'51bdf5fb2267d84ced002c99', | ||||||||||||||
| 155 | # 'name':'TEST_CACHE_01','size':0,'data_size':0} | ||||||||||||||
| 156 | # Fetched info about cache:{created_at => '0001-01-01T00:00:00Z',data_size => 0,id => '53083eb79ef7915a7f005bc0' | ||||||||||||||
| 157 | # ,name => 'name',project_id => '51bdf5fb2267d84ced002c99',size => 1,updated_at => '0001-01-01T00:00:00Z'} | ||||||||||||||
| 158 | 0 | $log->tracef('Exiting get_info_about_cache: %s', $info); | |||||||||||||
| 159 | 0 | return $info; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | |||||||||||||||
| 163 | sub get_cache { | ||||||||||||||
| 164 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
get_cache: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 165 | 0 | my %params = validate( | |||||||||||||
| 166 | @_, { | ||||||||||||||
| 167 | 'name' => { type => SCALAR, }, # cache name. | ||||||||||||||
| 168 | } | ||||||||||||||
| 169 | ); | ||||||||||||||
| 170 | 0 | $log->tracef('Entering get_cache(%s)', \%params); | |||||||||||||
| 171 | |||||||||||||||
| 172 | 0 | my $connection = $self->{'connection'}; | |||||||||||||
| 173 | my ($http_status_code, $response_message) = $connection->perform_iron_action( | ||||||||||||||
| 174 | IO::Iron::IronCache::Api::IRONCACHE_GET_INFO_ABOUT_A_CACHE(), | ||||||||||||||
| 175 | 0 | { '{Cache Name}' => $params{'name'}, } | |||||||||||||
| 176 | ); | ||||||||||||||
| 177 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||||||
| 178 | 0 | my $get_cache_name = $response_message->{'name'}; | |||||||||||||
| 179 | my $cache = IO::Iron::IronCache::Cache->new({ | ||||||||||||||
| 180 | 'ironcache_client' => $self, # Pass a reference to the parent object. | ||||||||||||||
| 181 | 'name' => $get_cache_name, | ||||||||||||||
| 182 | 'connection' => $self->{'connection'}, | ||||||||||||||
| 183 | 0 | 'policy' => $self->{'policy'}, | |||||||||||||
| 184 | }); | ||||||||||||||
| 185 | 0 | push @{$self->{'caches'}}, $cache; | |||||||||||||
| 186 | 0 | $log->debugf('Created a new IO::Iron::IronCache::Cache object (name=%s).', $get_cache_name); | |||||||||||||
| 187 | 0 | $log->tracef('Exiting get_cache: %s', $cache); | |||||||||||||
| 188 | 0 | return $cache; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | |||||||||||||||
| 192 | sub create_cache { | ||||||||||||||
| 193 | 2 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
create_cache: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 194 | my %params = validate( | ||||||||||||||
| 195 | @_, { | ||||||||||||||
| 196 | 'name' => { type => SCALAR, callbacks => { | ||||||||||||||
| 197 | 2 | ▶ | 'RFC 3986 reserved character check' => sub { return ! IO::Iron::Common::contains_rfc_3986_res_chars(shift) }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 198 | }}, # cache name. | ||||||||||||||
| 199 | } | ||||||||||||||
| 200 | 2 | ); | |||||||||||||
| 201 | 2 | $log->tracef('Entering create_cache(%s)', \%params); | |||||||||||||
| 202 | |||||||||||||||
| 203 | 2 | $self->validate_cache_name('name' => $params{'name'}); | |||||||||||||
| 204 | my $cache = IO::Iron::IronCache::Cache->new({ | ||||||||||||||
| 205 | 'ironcache_client' => $self, # Pass a reference to the parent object. | ||||||||||||||
| 206 | 'name' => $params{'name'}, | ||||||||||||||
| 207 | 'connection' => $self->{'connection'}, | ||||||||||||||
| 208 | 1 | 'policy' => $self->{'policy'}, | |||||||||||||
| 209 | }); | ||||||||||||||
| 210 | 1 | push @{$self->{'caches'}}, $cache; | |||||||||||||
| 211 | |||||||||||||||
| 212 | 1 | $log->debugf('Created a new IO::Iron::IronCache::Cache object (name=%s.)', $params{'name'}); | |||||||||||||
| 213 | 1 | $log->tracef('Exiting get_cache: %s', $cache); | |||||||||||||
| 214 | 1 | return $cache; | |||||||||||||
| 215 | } | ||||||||||||||
| 216 | |||||||||||||||
| 217 | |||||||||||||||
| 218 | sub delete_cache { | ||||||||||||||
| 219 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
delete_cache: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 220 | 0 | my %params = validate( | |||||||||||||
| 221 | @_, { | ||||||||||||||
| 222 | 'name' => { type => SCALAR, }, # cache name. | ||||||||||||||
| 223 | } | ||||||||||||||
| 224 | ); | ||||||||||||||
| 225 | 0 | $log->tracef('Entering delete_cache(%s)', \%params); | |||||||||||||
| 226 | |||||||||||||||
| 227 | 0 | my $connection = $self->{'connection'}; | |||||||||||||
| 228 | my ($http_status_code) = $connection->perform_iron_action( | ||||||||||||||
| 229 | IO::Iron::IronCache::Api::IRONCACHE_DELETE_A_CACHE(), | ||||||||||||||
| 230 | { | ||||||||||||||
| 231 | 0 | '{Cache Name}' => $params{'name'}, | |||||||||||||
| 232 | } | ||||||||||||||
| 233 | ); | ||||||||||||||
| 234 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||||||
| 235 | 0 | @{$self->{'caches'}} = grep { $_->name() ne $params{'name'} } @{$self->{'caches'}}; | |||||||||||||
| 236 | |||||||||||||||
| 237 | 0 | $log->debugf('Deleted cache (name=%s.)', $params{'name'}); | |||||||||||||
| 238 | 0 | $log->tracef('Exiting delete_cache: %d', 1); | |||||||||||||
| 239 | 0 | return 1; | |||||||||||||
| 240 | } | ||||||||||||||
| 241 | |||||||||||||||
| 242 | 6 | 1; | |||||||||||||
| 243 | |||||||||||||||
| 244 | __END__ | ||||||||||||||