| 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 | ▶ | use 5.010_000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # Global creator | ||||||||||
| 12 | ▶ | BEGIN { | |||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | # No exports | ||||||||||
| 14 | } | ||||||||||
| 15 | |||||||||||
| 16 | # Global destructor | ||||||||||
| 17 | ▶ | END { | |||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | # ABSTRACT: IronCache (Online Item-Value Storage) Client (Cache Item). | ||||||||||
| 22 | |||||||||||
| 23 | 3 | our $VERSION = '0.12_01'; # TRIAL VERSION: generated by DZP::OurPkgVersion | |||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | 3 | ▶ | use Log::Any qw($log); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 3 | ▶ | use Hash::Util 0.06 qw{lock_keys unlock_keys}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 3 | ▶ | use Carp::Assert::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 3 | ▶ | use English '-no_match_vars'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 3 | ▶ | use Params::Validate qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | # CONSTANTS for this module | ||||||||||
| 34 | |||||||||||
| 35 | # DEFAULTS | ||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | sub new { | ||||||||||
| 39 | 1 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||
| 40 | 1 | my %params = validate( | |||||||||
| 41 | @_, { | ||||||||||
| 42 | 'value' => { type => SCALAR, }, # Item value (free text), mandatory, can be empty. | ||||||||||
| 43 | 'expires_in' => { type => SCALAR, optional => 1, }, # How long in seconds to keep the item in the cache before it is deleted. | ||||||||||
| 44 | 'expires' => { type => SCALAR, optional => 1, }, # When will the item be deleted. This is a date string. | ||||||||||
| 45 | # TODO Add expires into docs! | ||||||||||
| 46 | 'replace' => { type => SCALAR, optional => 1, }, # Only set the item if the item is already in the cache. | ||||||||||
| 47 | 'add' => { type => SCALAR, optional => 1, }, # Only set the item if the item is not already in the cache. | ||||||||||
| 48 | 'cas' => { type => SCALAR, optional => 1, }, # Cas value can only be set when the item is read from the cache. | ||||||||||
| 49 | } | ||||||||||
| 50 | ); | ||||||||||
| 51 | 1 | $log->tracef('Entering new(%s, %s)', $class, %params); | |||||||||
| 52 | 1 | my $self; | |||||||||
| 53 | 1 | my @self_keys = ( ## no critic (CodeLayout::ProhibitQuotedWordLists) | |||||||||
| 54 | 'value', # Item value (free text), can be empty. | ||||||||||
| 55 | 'expires_in', # How long in seconds to keep the item in the cache before it is deleted. | ||||||||||
| 56 | 'expires', # When will the item be deleted. This is a date string. | ||||||||||
| 57 | 'replace', # Only set the item if the item is already in the cache. | ||||||||||
| 58 | 'add', # Only set the item if the item is not already in the cache. | ||||||||||
| 59 | 'cas', # Cas value can only be set when the item is read from the cache. | ||||||||||
| 60 | ); | ||||||||||
| 61 | 1 | lock_keys(%{$self}, @self_keys); | |||||||||
| 62 | 1 | ▶ | $self->{'value'} = defined $params{'value'} ? $params{'value'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 1 | ▶ | $self->{'expires_in'} = defined $params{'expires_in'} ? $params{'expires_in'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 1 | ▶ | $self->{'expires'} = defined $params{'expires'} ? $params{'expires'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 1 | ▶ | $self->{'replace'} = defined $params{'replace'} ? $params{'replace'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 1 | ▶ | $self->{'add'} = defined $params{'add'} ? $params{'add'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 1 | ▶ | $self->{'cas'} = defined $params{'cas'} ? $params{'cas'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | # All of the above can be undefined, except the value. | ||||||||||
| 69 | 1 | assert_defined( $self->{'value'}, 'self->{value} is defined and is not blank.' ); | |||||||||
| 70 | # If timeout, add or expires_in are undefined, the IronMQ defaults (at the server) will be used. | ||||||||||
| 71 | |||||||||||
| 72 | 1 | unlock_keys(%{$self}); | |||||||||
| 73 | 1 | my $blessed_ref = bless $self, $class; | |||||||||
| 74 | 1 | lock_keys(%{$self}, @self_keys); | |||||||||
| 75 | |||||||||||
| 76 | 1 | $log->tracef('Exiting new: %s', $blessed_ref); | |||||||||
| 77 | 1 | return $blessed_ref; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub value { return $_[0]->_access_internal('value', $_[1]); } | ||||||||
|
Subroutine
value: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 0 | ▶ | sub expires_in { return $_[0]->_access_internal('expires_in', $_[1]); } | ||||||||
|
Subroutine
expires_in: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 83 | 0 | ▶ | sub expires { return $_[0]->_access_internal('expires', $_[1]); } | ||||||||
|
Subroutine
expires: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 84 | 0 | ▶ | sub replace { return $_[0]->_access_internal('replace', $_[1]); } | ||||||||
|
Subroutine
replace: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 85 | 0 | ▶ | sub add { return $_[0]->_access_internal('add', $_[1]); } | ||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 86 | 0 | ▶ | sub cas { return $_[0]->_access_internal('cas', $_[1]); } | ||||||||
|
Subroutine
cas: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 87 | |||||||||||
| 88 | # TODO Move _access_internal() to IO::Iron::Common. | ||||||||||
| 89 | |||||||||||
| 90 | sub _access_internal { | ||||||||||
| 91 | 0 | ▶ | my ($self, $var_name, $var_value) = @_; | ||||||||
|
Subroutine
_access_internal: not called CC=2 SCAR=17.9
| |||||||||||
| 92 | 0 | $log->tracef('_access_internal(%s, %s)', $var_name, $var_value); | |||||||||
| 93 | 0 | ▶ | if( defined $var_value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | $self->{$var_name} = $var_value; | |||||||||
| 95 | 0 | return $self; | |||||||||
| 96 | } | ||||||||||
| 97 | else { | ||||||||||
| 98 | 0 | return $self->{$var_name}; | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 3 | 1; | |||||||||
| 103 | |||||||||||
| 104 | __END__ | ||||||||||