From d62d9ba627aa8dba59bd8b996490b7d790b5126b Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 31 Mar 2016 11:56:11 +0100 Subject: [PATCH] Bug 16166: Try to add readability --- Koha/Cache.pm | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Koha/Cache.pm b/Koha/Cache.pm index 5c918d0..7013ac0 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -281,14 +281,15 @@ sub set_in_cache { $expiry //= $self->{timeout}; my $set_sub = $self->{ref($self->{$cache}) . "_set"}; - my $flag = '-CF0'; # 0: scalar, 1: freezed data structure + my $flag = '-CF0'; # 0: scalar, 1: frozen data structure # Set in L1 cache if (ref($value)) { - $value = freeze($value); - $L1_cache{ $key } = [ (undef, $value) ]; + my $frozen= freeze($value); + $L1_cache{ $key } = { value => $value, frozen => $frozen }; + $value = $frozen; $flag = '-CF1'; } else { - $L1_cache{ $key } = $value; + $L1_cache{ $key } = { value => $value }; } $value .= $flag; @@ -329,16 +330,15 @@ sub get_from_cache { # Return L1 cache value if exists if ( exists $L1_cache{$key} ) { - if (ref($L1_cache{$key})) { + if (ref($L1_cache{$key}{value})) { if ($unsafe) { - $L1_cache{$key}->[0] ||= thaw($L1_cache{$key}->[1]); - return $L1_cache{$key}->[0]; + return $L1_cache{$key}->{value}; } else { - return thaw($L1_cache{$key}->[1]); + return thaw($L1_cache{$key}{frozen}); } } else { - # No need to deep copy if it's a scalar - return $L1_cache{$key}; + # No need to thaw if it's a scalar + return $L1_cache{$key}{value}; } } @@ -351,15 +351,16 @@ sub get_from_cache { my $flag = substr($value, -4, 4, ''); if ($flag eq '-CF0') { # it's a scalar - $L1_cache{$key} = $value; + $L1_cache{$key} = { value => $value }; return $value; } elsif ($flag eq '-CF1') { - # it's a freezed data structure + # it's a frozen data structure + my $frozen = freeze($value); + $L1_cache{$key} = { value => $value, frozen => $frozen }; + return $value; my $thawed; - eval { $thawed = thaw($value); }; + eval { $thawed = thaw($frozen); }; return if $@; - $L1_cache{$key} = [ (undef, $value) ]; - $L1_cache{$key}->[0] = $thawed if $unsafe; return $thawed; } -- 2.7.0