File Coverage

blib/lib/IO/Iron/IronCache/Item.pm
Criterion Covered Total %
statement 44 56 78.5
branch 6 14 42.8
condition n/a
subroutine 11 18 61.1
pod 7 7 100.0
total 68 95 71.5


line stmt bran cond sub pod time code
1             package IO::Iron::IronCache::Item;
2              
3             ## no critic (Documentation::RequirePodAtEnd)
4             ## no critic (Documentation::RequirePodSections)
5             ## no critic (Subroutines::RequireArgUnpacking)
6              
7 3     3   7015 use 5.010_000;
  3         16  
8 3     3   20 use strict;
  3         7  
  3         61  
9 3     3   20 use warnings;
  3         5  
  3         75  
10              
11             # Global creator
12       3     BEGIN {
13             # No exports
14             }
15              
16             # Global destructor
17       3     END {
18             }
19              
20             # ABSTRACT: IronCache (Online Item-Value Storage) Client (Cache Item).
21              
22             our $VERSION = '0.14'; # VERSION: generated by DZP::OurPkgVersion
23              
24 3     3   18 use Log::Any qw($log);
  3         11  
  3         15  
25 3     3   614 use Hash::Util 0.06 qw{lock_keys unlock_keys};
  3         79  
  3         18  
26 3     3   192 use Carp::Assert::More;
  3         11  
  3         620  
27 3     3   22 use English '-no_match_vars';
  3         9  
  3         17  
28 3     3   1106 use Params::Validate qw(:all);
  3         9  
  3         2239  
29              
30             # CONSTANTS for this module
31              
32             # DEFAULTS
33              
34             sub new {
35 1     1 1 565 my $class = shift;
36 1         35 my %params = validate(
37             @_,
38             {
39             'value' => { type => SCALAR, }, # Item value (free text), mandatory, can be empty.
40             'expires_in' => { type => SCALAR, optional => 1, }
41             , # How long in seconds to keep the item in the cache before it is deleted.
42             'expires' => { type => SCALAR, optional => 1, }, # When will the item be deleted. This is a date string.
43             # TODO Add expires into docs!
44             'replace' => { type => SCALAR, optional => 1, }, # Only set the item if the item is already in the cache.
45             'add' => { type => SCALAR, optional => 1, }, # Only set the item if the item is not already in the cache.
46             'cas' => { type => SCALAR, optional => 1, }, # Cas value can only be set when the item is read from the cache.
47             }
48             );
49 1         10 $log->tracef( 'Entering new(%s, %s)', $class, %params );
50 1         86 my $self;
51 1         5 my @self_keys = ( ## no critic (CodeLayout::ProhibitQuotedWordLists)
52             'value', # Item value (free text), can be empty.
53             'expires_in', # How long in seconds to keep the item in the cache before it is deleted.
54             'expires', # When will the item be deleted. This is a date string.
55             'replace', # Only set the item if the item is already in the cache.
56             'add', # Only set the item if the item is not already in the cache.
57             'cas', # Cas value can only be set when the item is read from the cache.
58             );
59 1         3 lock_keys( %{$self}, @self_keys );
  1         4  
60 1 50       60 $self->{'value'} = defined $params{'value'} ? $params{'value'} : undef;
61 1 50       6 $self->{'expires_in'} = defined $params{'expires_in'} ? $params{'expires_in'} : undef;
62 1 50       4 $self->{'expires'} = defined $params{'expires'} ? $params{'expires'} : undef;
63 1 50       3 $self->{'replace'} = defined $params{'replace'} ? $params{'replace'} : undef;
64 1 50       4 $self->{'add'} = defined $params{'add'} ? $params{'add'} : undef;
65 1 50       4 $self->{'cas'} = defined $params{'cas'} ? $params{'cas'} : undef;
66              
67             # All of the above can be undefined, except the value.
68 1         5 assert_defined( $self->{'value'}, 'self->{value} is defined and is not blank.' );
69              
70             # If timeout, add or expires_in are undefined, the IronMQ defaults (at the server) will be used.
71              
72 1         4 unlock_keys( %{$self} );
  1         4  
73 1         9 my $blessed_ref = bless $self, $class;
74 1         2 lock_keys( %{$self}, @self_keys );
  1         9  
75              
76 1         94 $log->tracef( 'Exiting new: %s', $blessed_ref );
77 1         291 return $blessed_ref;
78             }
79              
80 0     0 1   sub value { return $_[0]->_access_internal( 'value', $_[1] ); }
81 0     0 1   sub expires_in { return $_[0]->_access_internal( 'expires_in', $_[1] ); }
82 0     0 1   sub expires { return $_[0]->_access_internal( 'expires', $_[1] ); }
83 0     0 1   sub replace { return $_[0]->_access_internal( 'replace', $_[1] ); }
84 0     0 1   sub add { return $_[0]->_access_internal( 'add', $_[1] ); }
85 0     0 1   sub cas { return $_[0]->_access_internal( 'cas', $_[1] ); }
86              
87             # TODO Move _access_internal() to IO::Iron::Common.
88              
89             sub _access_internal {
90 0     0     my ( $self, $var_name, $var_value ) = @_;
91 0           $log->tracef( '_access_internal(%s, %s)', $var_name, $var_value );
92 0 0         if ( defined $var_value ) {
93 0           $self->{$var_name} = $var_value;
94 0           return $self;
95             }
96             else {
97 0           return $self->{$var_name};
98             }
99             }
100              
101             1;
102              
103             __END__
104              
105             =pod
106              
107             =encoding UTF-8
108              
109             =head1 NAME
110              
111             IO::Iron::IronCache::Item - IronCache (Online Item-Value Storage) Client (Cache Item).
112              
113             =head1 VERSION
114              
115             version 0.14
116              
117             =head1 SYNOPSIS
118              
119             Please see IO::Iron::IronCache::Client for usage.
120              
121             =for stopwords IronCache Params IronHTTPCallException Mikko Koivunalho timestamp cas Cas
122              
123             =head1 REQUIREMENTS
124              
125             =head1 SUBROUTINES/METHODS
126              
127             =head2 new
128              
129             Creator function.
130              
131             =head2 Getters/setters
132              
133             Set or get a property.
134             When setting, returns the reference to the object.
135              
136             =over 8
137              
138             =item value Item value (free text), can be empty.
139              
140             =item expires_in How long in seconds to keep the item in the cache before it is deleted.
141              
142             =item expires Time of expiration as a timestamp.
143              
144             =item replace Only set the item if the item is already in the cache.
145              
146             =item add Only set the item if the item is not already in the cache.
147              
148             =item cas Cas value can only be set when the item is read from the cache.
149              
150             =back
151              
152             =head1 AUTHOR
153              
154             Mikko Koivunalho <mikko.koivunalho@iki.fi>
155              
156             =head1 BUGS
157              
158             Please report any bugs or feature requests to bug-io-iron@rt.cpan.org or through the web interface at:
159             http://rt.cpan.org/Public/Dist/Display.html?Name=IO-Iron
160              
161             =head1 COPYRIGHT AND LICENSE
162              
163             This software is copyright (c) 2023 by Mikko Koivunalho.
164              
165             This is free software; you can redistribute it and/or modify it under
166             the same terms as the Perl 5 programming language system itself.
167              
168             The full text of the license can be found in the
169             F<LICENSE> file included with this distribution.
170              
171             =cut