| 1 | package IO::Iron::IronCache::Cache; | ||||||||||
| 2 | |||||||||||
| 3 | ## no critic (Documentation::RequirePodAtEnd) | ||||||||||
| 4 | ## no critic (Documentation::RequirePodSections) | ||||||||||
| 5 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||||||||||
| 6 | ## no critic (Subroutines::RequireArgUnpacking) | ||||||||||
| 7 | |||||||||||
| 8 | 6 | ▶ | use 5.010_000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | # Global creator | ||||||||||
| 13 | BEGIN { | ||||||||||
| 14 | # Export Nothing | ||||||||||
| 15 | 6 | ▶ | use parent qw(IO::Iron::IronCache::Policy); # Inheritance | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: not called CC=1 SCAR=0.0
| |||||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | # Global destructor | ||||||||||
| 19 | ▶ | END { | |||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | # ABSTRACT: IronCache (Online Item-Value Storage) Client (Cache). | ||||||||||
| 24 | |||||||||||
| 25 | 6 | our $VERSION = '0.12_01'; # TRIAL VERSION: generated by DZP::OurPkgVersion | |||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | 6 | ▶ | use Log::Any qw($log); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 6 | ▶ | use Hash::Util 0.06 qw{lock_keys unlock_keys}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 6 | ▶ | use Carp::Assert::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 6 | ▶ | use English '-no_match_vars'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 6 | ▶ | use Params::Validate qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 6 | ▶ | use IO::Iron::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 6 | ▶ | use IO::Iron::IronCache::Api; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | # CONSTANTS for this module | ||||||||||
| 39 | |||||||||||
| 40 | # DEFAULTS | ||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | sub new { | ||||||||||
| 45 | 1 | ▶ | my ($class, $params) = @_; | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 46 | 1 | $log->tracef('Entering new(%s, %s)', $class, $params); | |||||||||
| 47 | 1 | my $self; | |||||||||
| 48 | 1 | my @self_keys = ( ## no critic (CodeLayout::ProhibitQuotedWordLists) | |||||||||
| 49 | 'ironcache_client', # Reference to IronCache client | ||||||||||
| 50 | 'name', # Cache name | ||||||||||
| 51 | 'connection', # Reference to REST client | ||||||||||
| 52 | 'policy', # The policies. (copied from the client.) | ||||||||||
| 53 | 'last_http_status_code', # After successfull network operation, the return value is here. | ||||||||||
| 54 | ); | ||||||||||
| 55 | 1 | lock_keys(%{$self}, @self_keys); | |||||||||
| 56 | 1 | ▶ | $self->{'ironcache_client'} = defined $params->{'ironcache_client'} ? $params->{'ironcache_client'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 1 | ▶ | $self->{'name'} = defined $params->{'name'} ? $params->{'name'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 1 | ▶ | $self->{'connection'} = defined $params->{'connection'} ? $params->{'connection'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 1 | ▶ | $self->{'policy'} = defined $params->{'policy'} ? $params->{'policy'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 1 | assert_isa( $self->{'ironcache_client'}, 'IO::Iron::IronCache::Client' , 'self->{ironcache_client} is IO::Iron::IronCache::Client.'); | |||||||||
| 61 | 1 | assert_nonblank( $self->{'name'}, 'self->{name} is defined and is not blank.' ); | |||||||||
| 62 | 1 | assert_isa( $self->{'connection'}, 'IO::Iron::Connection' , 'self->{connection} is IO::Iron::Connection.'); | |||||||||
| 63 | 1 | assert_hashref( $self->{'policy'}, 'self->{policy} is a reference to a hash.'); | |||||||||
| 64 | |||||||||||
| 65 | 1 | unlock_keys(%{$self}); | |||||||||
| 66 | 1 | my $blessed_ref = bless $self, $class; | |||||||||
| 67 | 1 | lock_keys(%{$self}, @self_keys); | |||||||||
| 68 | |||||||||||
| 69 | 1 | $log->tracef('Exiting new: %s', $blessed_ref); | |||||||||
| 70 | 1 | return $blessed_ref; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | sub name { return $_[0]->_access_internal('name', $_[1]); } | ||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 75 | |||||||||||
| 76 | # TODO Move _access_internal() to IO::Iron::Common. | ||||||||||
| 77 | |||||||||||
| 78 | sub _access_internal { | ||||||||||
| 79 | 0 | ▶ | my ($self, $var_name, $var_value) = @_; | ||||||||
|
Subroutine
_access_internal: not called CC=2 SCAR=17.9
| |||||||||||
| 80 | 0 | $log->tracef('_access_internal(%s, %s)', $var_name, $var_value); | |||||||||
| 81 | 0 | ▶ | if( defined $var_value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | $self->{$var_name} = $var_value; | |||||||||
| 83 | 0 | return $self; | |||||||||
| 84 | } | ||||||||||
| 85 | else { | ||||||||||
| 86 | 0 | return $self->{$var_name}; | |||||||||
| 87 | } | ||||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | sub clear { | ||||||||||
| 92 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 93 | 0 | my %params = validate( | |||||||||
| 94 | @_, { | ||||||||||
| 95 | # No parameters | ||||||||||
| 96 | } | ||||||||||
| 97 | ); | ||||||||||
| 98 | 0 | $log->tracef('Entering clear()'); | |||||||||
| 99 | |||||||||||
| 100 | 0 | my $cache_name = $self->name(); | |||||||||
| 101 | 0 | my $connection = $self->{'connection'}; | |||||||||
| 102 | 0 | my ($http_status_code, $response_message) = $connection->perform_iron_action( | |||||||||
| 103 | IO::Iron::IronCache::Api::IRONCACHE_CLEAR_A_CACHE(), | ||||||||||
| 104 | { | ||||||||||
| 105 | '{Cache Name}' => $cache_name, | ||||||||||
| 106 | } | ||||||||||
| 107 | ); | ||||||||||
| 108 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||
| 109 | |||||||||||
| 110 | 0 | $log->tracef('Exiting clear: %d', 1); | |||||||||
| 111 | 0 | return 1; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | sub put { | ||||||||||
| 116 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
put: called CC=3 SCAR=18.5
Pod
documented
| |||||||||||
| 117 | my %params = validate( | ||||||||||
| 118 | @_, { | ||||||||||
| 119 | 'key' => { type => SCALAR, callbacks => { | ||||||||||
| 120 | 1 | ▶ | 'RFC 3986 reserved character check' => sub { return ! IO::Iron::Common::contains_rfc_3986_res_chars(shift) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 121 | }}, # cache item key. | ||||||||||
| 122 | 1 | 'item' => { isa => 'IO::Iron::IronCache::Item', }, # cache item. | |||||||||
| 123 | } | ||||||||||
| 124 | ); | ||||||||||
| 125 | 1 | $log->tracef('Entering put(%s)', \%params); | |||||||||
| 126 | |||||||||||
| 127 | 1 | $self->validate_item_key('key' => $params{'key'}); | |||||||||
| 128 | 0 | my $cache_name = $self->name(); | |||||||||
| 129 | 0 | my $connection = $self->{'connection'}; | |||||||||
| 130 | 0 | my %item_body; | |||||||||
| 131 | 0 | foreach my $field_name (keys %{ IO::Iron::IronCache::Api::IRONCACHE_PUT_AN_ITEM_INTO_A_CACHE()->{'request_fields'}}) { | |||||||||
| 132 | 0 | ▶ | if (defined $params{'item'}->{$field_name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 0 | $item_body{$field_name} = $params{'item'}->{$field_name}; | |||||||||
| 134 | }; | ||||||||||
| 135 | } | ||||||||||
| 136 | my ($http_status_code, $response_message) = $connection->perform_iron_action( | ||||||||||
| 137 | IO::Iron::IronCache::Api::IRONCACHE_PUT_AN_ITEM_INTO_A_CACHE(), | ||||||||||
| 138 | { | ||||||||||
| 139 | '{Cache Name}' => $cache_name, | ||||||||||
| 140 | 0 | '{Key}' => $params{'key'}, | |||||||||
| 141 | 'body' => \%item_body, | ||||||||||
| 142 | } | ||||||||||
| 143 | ); | ||||||||||
| 144 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||
| 145 | |||||||||||
| 146 | 0 | $log->tracef('Exiting put: %d', 1); | |||||||||
| 147 | 0 | return 1; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | |||||||||||
| 151 | # TODO Correct documentation: if item does not exist, it is created. | ||||||||||
| 152 | |||||||||||
| 153 | sub increment { | ||||||||||
| 154 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
increment: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 155 | my %params = validate( | ||||||||||
| 156 | @_, { | ||||||||||
| 157 | 'key' => { type => SCALAR, callbacks => { | ||||||||||
| 158 | 0 | ▶ | 'RFC 3986 reserved character check' => sub { return ! IO::Iron::Common::contains_rfc_3986_res_chars(shift) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 159 | }}, # cache item key. | ||||||||||
| 160 | 0 | 'increment' => { type => SCALAR, }, # cache item increment. | |||||||||
| 161 | } | ||||||||||
| 162 | ); | ||||||||||
| 163 | 0 | assert_nonblank( $params{'key'}, 'key is defined and is not blank.' ); | |||||||||
| 164 | 0 | assert_integer( $params{'increment'}, 'increment amount is integer.'); | |||||||||
| 165 | 0 | $log->tracef('Entering increment(%s)', \%params); | |||||||||
| 166 | |||||||||||
| 167 | 0 | $self->validate_item_key('key' => $params{'key'}); | |||||||||
| 168 | 0 | my $cache_name = $self->name(); | |||||||||
| 169 | 0 | my $connection = $self->{'connection'}; | |||||||||
| 170 | 0 | my %item_body; | |||||||||
| 171 | 0 | $item_body{'amount'} = $params{'increment'}; | |||||||||
| 172 | my ($http_status_code, $response_message) = $connection->perform_iron_action( | ||||||||||
| 173 | IO::Iron::IronCache::Api::IRONCACHE_INCREMENT_AN_ITEMS_VALUE(), | ||||||||||
| 174 | { | ||||||||||
| 175 | '{Cache Name}' => $cache_name, | ||||||||||
| 176 | 0 | '{Key}' => $params{'key'}, | |||||||||
| 177 | 'body' => \%item_body, | ||||||||||
| 178 | } | ||||||||||
| 179 | ); | ||||||||||
| 180 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||
| 181 | 0 | my $new_value = $response_message->{'value'}; | |||||||||
| 182 | |||||||||||
| 183 | 0 | $log->tracef('Exiting increment: %d', $new_value); | |||||||||
| 184 | 0 | return $new_value; | |||||||||
| 185 | } | ||||||||||
| 186 | |||||||||||
| 187 | |||||||||||
| 188 | sub get { | ||||||||||
| 189 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 190 | 0 | my %params = validate( | |||||||||
| 191 | @_, { | ||||||||||
| 192 | 'key' => { type => SCALAR, }, # cache item key. | ||||||||||
| 193 | } | ||||||||||
| 194 | ); | ||||||||||
| 195 | 0 | assert_nonblank( $params{'key'}, 'key is defined and is not blank.' ); | |||||||||
| 196 | 0 | $log->tracef('Entering get(%s)', \%params); | |||||||||
| 197 | |||||||||||
| 198 | 0 | my $cache_name = $self->name(); | |||||||||
| 199 | 0 | my $connection = $self->{'connection'}; | |||||||||
| 200 | my ($http_status_code, $response_message) = $connection->perform_iron_action( | ||||||||||
| 201 | IO::Iron::IronCache::Api::IRONCACHE_GET_AN_ITEM_FROM_A_CACHE(), | ||||||||||
| 202 | { | ||||||||||
| 203 | '{Cache Name}' => $cache_name, | ||||||||||
| 204 | 0 | '{Key}' => $params{'key'}, | |||||||||
| 205 | } | ||||||||||
| 206 | ); | ||||||||||
| 207 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||
| 208 | my $new_item = IO::Iron::IronCache::Item->new( | ||||||||||
| 209 | 'value' => $response_message->{'value'}, | ||||||||||
| 210 | 0 | 'cas' => $response_message->{'cas'}, | |||||||||
| 211 | ); | ||||||||||
| 212 | 0 | ▶ | $new_item->expires($response_message->{'expires'}) if defined $response_message->{'expires'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | 0 | ▶ | $new_item->replace($response_message->{'replace'}) if defined $response_message->{'replace'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 214 | 0 | ▶ | $new_item->add($response_message->{'add'}) if defined $response_message->{'add'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | |||||||||||
| 216 | 0 | $log->tracef('Exiting get: %s', $new_item); | |||||||||
| 217 | 0 | return $new_item; | |||||||||
| 218 | } | ||||||||||
| 219 | |||||||||||
| 220 | |||||||||||
| 221 | sub delete { ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||||||||||
| 222 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 223 | 0 | my %params = validate( | |||||||||
| 224 | @_, { | ||||||||||
| 225 | 'key' => { type => SCALAR, }, # cache item key. | ||||||||||
| 226 | } | ||||||||||
| 227 | ); | ||||||||||
| 228 | 0 | assert_nonblank( $params{'key'}, 'key is defined and is not blank.' ); | |||||||||
| 229 | 0 | $log->tracef('Entering delete(%s)', \%params); | |||||||||
| 230 | |||||||||||
| 231 | 0 | my $cache_name = $self->name(); | |||||||||
| 232 | 0 | my $connection = $self->{'connection'}; | |||||||||
| 233 | my ($http_status_code, $response_message) = $connection->perform_iron_action( | ||||||||||
| 234 | IO::Iron::IronCache::Api::IRONCACHE_DELETE_AN_ITEM_FROM_A_CACHE(), | ||||||||||
| 235 | { | ||||||||||
| 236 | '{Cache Name}' => $cache_name, | ||||||||||
| 237 | 0 | '{Key}' => $params{'key'}, | |||||||||
| 238 | } | ||||||||||
| 239 | ); | ||||||||||
| 240 | 0 | $self->{'last_http_status_code'} = $http_status_code; | |||||||||
| 241 | |||||||||||
| 242 | 0 | $log->tracef('Exiting delete: %d', 1); | |||||||||
| 243 | 0 | return 1; | |||||||||
| 244 | } | ||||||||||
| 245 | |||||||||||
| 246 | 6 | 1; | |||||||||
| 247 | |||||||||||
| 248 | __END__ | ||||||||||