- my $value = $get_sub ? $get_sub->($key) : $self->{$cache}->get($key);
-
- # Update the L1 cache when fetching the L2 cache
- # Otherwise the L1 cache won't ever be populated
- $L1_cache{$key} = $value;
-
- $value = clone $value if ref $L1_cache{$key} and not $unsafe;
+ my $L2_value = $get_sub ? $get_sub->($key) : $self->{$cache}->get($key);
+
+ return if ref($L2_value);
+ return unless (defined($L2_value) && length($L2_value) >= 4);
+
+ my $flag = substr($L2_value, -4, 4, '');
+ if ($flag eq '-CF0') {
+ # it's a scalar
+ $L1_cache{$self->{namespace}}{$key} = $L2_value;
+ return $L2_value;
+ } elsif ($flag eq '-CF1') {
+ # it's a frozen data structure
+ my $thawed;
+ eval { $thawed = $L1_decoder->decode($L2_value); };
+ return if $@;
+ $L1_cache{$self->{namespace}}{$key}->{frozen} = $L2_value;
+ # ONLY save thawed for unsafe calls !!!
+ $L1_cache{$self->{namespace}}{$key}->{thawed} = $thawed if $unsafe;
+ return $thawed;
+ }