File Coverage

blib/lib/IO/Iron/IronCache/Cache.pm
Criterion Covered Total %
statement 58 126 46.0
branch 4 18 22.2
condition n/a
subroutine 15 23 65.2
pod 7 7 100.0
total 84 174 48.2


line stmt bran cond sub pod time code
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     6   108 use 5.010_000;
  6         24  
9 6     6   36 use strict;
  6         17  
  6         127  
10 6     6   30 use warnings;
  6         13  
  6         213  
11              
12             # Global creator
13 0         0 BEGIN {
14             # Export Nothing
15 6     6   33 use parent qw(IO::Iron::IronCache::Policy); # Inheritance
  6     0   36  
  6         48  
16             }
17              
18             # Global destructor
19       6     END {
20             }
21              
22             # ABSTRACT: IronCache (Online Item-Value Storage) Client (Cache).
23              
24             our $VERSION = '0.14'; # VERSION: generated by DZP::OurPkgVersion
25              
26 6     6   765 use Log::Any qw($log);
  6         11  
  6         53  
27 6     6   1211 use Hash::Util 0.06 qw{lock_keys unlock_keys};
  6         144  
  6         36  
28 6     6   446 use Carp::Assert::More;
  6         10  
  6         1206  
29 6     6   59 use English '-no_match_vars';
  6         30  
  6         48  
30 6     6   2289 use Params::Validate qw(:all);
  6         15  
  6         931  
31              
32 6     6   42 use IO::Iron::Common;
  6         13  
  6         220  
33 6     6   40 use IO::Iron::IronCache::Api;
  6         12  
  6         8657  
34              
35             # CONSTANTS for this module
36              
37             # DEFAULTS
38              
39             sub new {
40 1     1 1 3 my ( $class, $params ) = @_;
41 1         15 $log->tracef( 'Entering new(%s, %s)', $class, $params );
42 1         364 my $self;
43 1         5 my @self_keys = ( ## no critic (CodeLayout::ProhibitQuotedWordLists)
44             'ironcache_client', # Reference to IronCache client
45             'name', # Cache name
46             'connection', # Reference to REST client
47             'policy', # The policies. (copied from the client.)
48             'last_http_status_code', # After successfull network operation, the return value is here.
49             );
50 1         3 lock_keys( %{$self}, @self_keys );
  1         4  
51 1 50       72 $self->{'ironcache_client'} = defined $params->{'ironcache_client'} ? $params->{'ironcache_client'} : undef;
52 1 50       5 $self->{'name'} = defined $params->{'name'} ? $params->{'name'} : undef;
53 1 50       5 $self->{'connection'} = defined $params->{'connection'} ? $params->{'connection'} : undef;
54 1 50       6 $self->{'policy'} = defined $params->{'policy'} ? $params->{'policy'} : undef;
55 1         6 assert_isa( $self->{'ironcache_client'},
56             'IO::Iron::IronCache::Client', 'self->{ironcache_client} is IO::Iron::IronCache::Client.' );
57 1         21 assert_nonblank( $self->{'name'}, 'self->{name} is defined and is not blank.' );
58 1         17 assert_isa( $self->{'connection'}, 'IO::Iron::Connection', 'self->{connection} is IO::Iron::Connection.' );
59 1         16 assert_hashref( $self->{'policy'}, 'self->{policy} is a reference to a hash.' );
60              
61 1         8 unlock_keys( %{$self} );
  1         4  
62 1         11 my $blessed_ref = bless $self, $class;
63 1         4 lock_keys( %{$self}, @self_keys );
  1         13  
64              
65 1         101 $log->tracef( 'Exiting new: %s', $blessed_ref );
66 1         424 return $blessed_ref;
67             }
68              
69 0     0 1 0 sub name { return $_[0]->_access_internal( 'name', $_[1] ); }
70              
71             # TODO Move _access_internal() to IO::Iron::Common.
72              
73             sub _access_internal {
74 0     0   0 my ( $self, $var_name, $var_value ) = @_;
75 0         0 $log->tracef( '_access_internal(%s, %s)', $var_name, $var_value );
76 0 0       0 if ( defined $var_value ) {
77 0         0 $self->{$var_name} = $var_value;
78 0         0 return $self;
79             }
80             else {
81 0         0 return $self->{$var_name};
82             }
83             }
84              
85             sub clear {
86 0     0 1 0 my $self = shift;
87 0         0 my %params = validate(
88             @_,
89             {
90             # No parameters
91             }
92             );
93 0         0 $log->tracef('Entering clear()');
94              
95 0         0 my $cache_name = $self->name();
96 0         0 my $connection = $self->{'connection'};
97 0         0 my ( $http_status_code, $response_message ) = $connection->perform_iron_action(
98             IO::Iron::IronCache::Api::IRONCACHE_CLEAR_A_CACHE(),
99             {
100             '{Cache Name}' => $cache_name,
101             }
102             );
103 0         0 $self->{'last_http_status_code'} = $http_status_code;
104              
105 0         0 $log->tracef( 'Exiting clear: %d', 1 );
106 0         0 return 1;
107             }
108              
109             sub put {
110 1     1 1 66 my $self = shift;
111             my %params = validate(
112             @_,
113             {
114             'key' => {
115             type => SCALAR,
116             callbacks => {
117 1     1   6 'RFC 3986 reserved character check' => sub { return !IO::Iron::Common::contains_rfc_3986_res_chars(shift) },
118             }
119             }, # cache item key.
120 1         32 'item' => { isa => 'IO::Iron::IronCache::Item', }, # cache item.
121             }
122             );
123 1         17 $log->tracef( 'Entering put(%s)', \%params );
124              
125 1         267 $self->validate_item_key( 'key' => $params{'key'} );
126 0           my $cache_name = $self->name();
127 0           my $connection = $self->{'connection'};
128 0           my %item_body;
129 0           foreach my $field_name ( keys %{ IO::Iron::IronCache::Api::IRONCACHE_PUT_AN_ITEM_INTO_A_CACHE()->{'request_fields'} } ) {
  0            
130 0 0         if ( defined $params{'item'}->{$field_name} ) {
131 0           $item_body{$field_name} = $params{'item'}->{$field_name};
132             }
133             }
134             my ( $http_status_code, $response_message ) = $connection->perform_iron_action(
135             IO::Iron::IronCache::Api::IRONCACHE_PUT_AN_ITEM_INTO_A_CACHE(),
136             {
137             '{Cache Name}' => $cache_name,
138 0           '{Key}' => $params{'key'},
139             'body' => \%item_body,
140             }
141             );
142 0           $self->{'last_http_status_code'} = $http_status_code;
143              
144 0           $log->tracef( 'Exiting put: %d', 1 );
145 0           return 1;
146             }
147              
148             # TODO Correct documentation: if item does not exist, it is created.
149              
150             sub increment {
151 0     0 1   my $self = shift;
152             my %params = validate(
153             @_,
154             {
155             'key' => {
156             type => SCALAR,
157             callbacks => {
158 0     0     'RFC 3986 reserved character check' => sub { return !IO::Iron::Common::contains_rfc_3986_res_chars(shift) },
159             }
160             }, # cache item key.
161 0           'increment' => { type => SCALAR, }, # cache item increment.
162             }
163             );
164 0           assert_nonblank( $params{'key'}, 'key is defined and is not blank.' );
165 0           assert_integer( $params{'increment'}, 'increment amount is integer.' );
166 0           $log->tracef( 'Entering increment(%s)', \%params );
167              
168 0           $self->validate_item_key( 'key' => $params{'key'} );
169 0           my $cache_name = $self->name();
170 0           my $connection = $self->{'connection'};
171 0           my %item_body;
172 0           $item_body{'amount'} = $params{'increment'};
173             my ( $http_status_code, $response_message ) = $connection->perform_iron_action(
174             IO::Iron::IronCache::Api::IRONCACHE_INCREMENT_AN_ITEMS_VALUE(),
175             {
176             '{Cache Name}' => $cache_name,
177 0           '{Key}' => $params{'key'},
178             'body' => \%item_body,
179             }
180             );
181 0           $self->{'last_http_status_code'} = $http_status_code;
182 0           my $new_value = $response_message->{'value'};
183              
184 0           $log->tracef( 'Exiting increment: %d', $new_value );
185 0           return $new_value;
186             }
187              
188             sub get {
189 0     0 1   my $self = shift;
190 0           my %params = validate(
191             @_,
192             {
193             'key' => { type => SCALAR, }, # cache item key.
194             }
195             );
196 0           assert_nonblank( $params{'key'}, 'key is defined and is not blank.' );
197 0           $log->tracef( 'Entering get(%s)', \%params );
198              
199 0           my $cache_name = $self->name();
200 0           my $connection = $self->{'connection'};
201             my ( $http_status_code, $response_message ) = $connection->perform_iron_action(
202             IO::Iron::IronCache::Api::IRONCACHE_GET_AN_ITEM_FROM_A_CACHE(),
203             {
204             '{Cache Name}' => $cache_name,
205 0           '{Key}' => $params{'key'},
206             }
207             );
208 0           $self->{'last_http_status_code'} = $http_status_code;
209             my $new_item = IO::Iron::IronCache::Item->new(
210             'value' => $response_message->{'value'},
211 0           'cas' => $response_message->{'cas'},
212             );
213 0 0         $new_item->expires( $response_message->{'expires'} ) if defined $response_message->{'expires'};
214 0 0         $new_item->replace( $response_message->{'replace'} ) if defined $response_message->{'replace'};
215 0 0         $new_item->add( $response_message->{'add'} ) if defined $response_message->{'add'};
216              
217 0           $log->tracef( 'Exiting get: %s', $new_item );
218 0           return $new_item;
219             }
220              
221             sub delete { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
222 0     0 1   my $self = shift;
223 0           my %params = validate(
224             @_,
225             {
226             'key' => { type => SCALAR, }, # cache item key.
227             }
228             );
229 0           assert_nonblank( $params{'key'}, 'key is defined and is not blank.' );
230 0           $log->tracef( 'Entering delete(%s)', \%params );
231              
232 0           my $cache_name = $self->name();
233 0           my $connection = $self->{'connection'};
234             my ( $http_status_code, $response_message ) = $connection->perform_iron_action(
235             IO::Iron::IronCache::Api::IRONCACHE_DELETE_AN_ITEM_FROM_A_CACHE(),
236             {
237             '{Cache Name}' => $cache_name,
238 0           '{Key}' => $params{'key'},
239             }
240             );
241 0           $self->{'last_http_status_code'} = $http_status_code;
242              
243 0           $log->tracef( 'Exiting delete: %d', 1 );
244 0           return 1;
245             }
246              
247             1;
248              
249             __END__
250              
251             =pod
252              
253             =encoding UTF-8
254              
255             =head1 NAME
256              
257             IO::Iron::IronCache::Cache - IronCache (Online Item-Value Storage) Client (Cache).
258              
259             =head1 VERSION
260              
261             version 0.14
262              
263             =head1 SYNOPSIS
264              
265             Please see IO::Iron::IronCache::Client for usage.
266              
267             =for stopwords IronCache Params IronHTTPCallException Mikko Koivunalho
268              
269             =head1 REQUIREMENTS
270              
271             =head1 SUBROUTINES/METHODS
272              
273             =head2 new
274              
275             =over
276              
277             =item Creator function.
278              
279             =back
280              
281             =head2 Getters/setters
282              
283             Set or get a property.
284             When setting, returns the reference to the object.
285              
286             =over 8
287              
288             =item name Cache name.
289              
290             =back
291              
292             =head2 clear
293              
294             Deletes all items in an IronCache cache.
295              
296             =over 8
297              
298             =item Params: [NONE]
299              
300             =item Return: 1 == success.
301              
302             =item Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
303              
304             =back
305              
306             =head2 put
307              
308             =over
309              
310             =item Params: key, IO::Iron::IronCache::Item object.
311              
312             =item Return: 1 == success.
313              
314             =item Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
315              
316             =back
317              
318             =head2 increment
319              
320             =over
321              
322             =item Params: key, increment (integer number, can be negative).
323              
324             =item Return: the new value.
325              
326             =item Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
327              
328             =back
329              
330             =head2 get
331              
332             =over
333              
334             =item Params: key.
335              
336             =item Return: IO::Iron::IronCache::Item object.
337              
338             =item Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
339              
340             =back
341              
342             =head2 delete
343              
344             =over
345              
346             =item Params: key.
347              
348             =item Return: 1 if successful.
349              
350             =item Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
351              
352             =back
353              
354             =head1 AUTHOR
355              
356             Mikko Koivunalho <mikko.koivunalho@iki.fi>
357              
358             =head1 BUGS
359              
360             Please report any bugs or feature requests to bug-io-iron@rt.cpan.org or through the web interface at:
361             http://rt.cpan.org/Public/Dist/Display.html?Name=IO-Iron
362              
363             =head1 COPYRIGHT AND LICENSE
364              
365             This software is copyright (c) 2023 by Mikko Koivunalho.
366              
367             This is free software; you can redistribute it and/or modify it under
368             the same terms as the Perl 5 programming language system itself.
369              
370             The full text of the license can be found in the
371             F<LICENSE> file included with this distribution.
372              
373             =cut