View | Details | Raw Unified | Return to bug 16044
Collapse All | Expand All

(-)a/Koha/Cache.pm (-5 / +12 lines)
Lines 299-323 sub set_in_cache { Link Here
299
299
300
=head2 get_from_cache
300
=head2 get_from_cache
301
301
302
    my $value = $cache->get_from_cache($key);
302
    my $value = $cache->get_from_cache($key, [ $options ]);
303
303
304
Retrieve the value stored under the specified key in the default cache.
304
Retrieve the value stored under the specified key in the default cache.
305
305
306
The options can set an unsafe flag to avoid a deep copy.
307
When this flag is set, you have to know what you are doing!
308
If you are retrieving a structure and modify it, you will modify the contain
309
of the cache!
310
306
=cut
311
=cut
307
312
308
sub get_from_cache {
313
sub get_from_cache {
309
    my ( $self, $key, $cache ) = @_;
314
    my ( $self, $key, $options ) = @_;
315
    my $cache  = $options->{cache}  || 'cache';
316
    my $unsafe = $options->{unsafe} || 0;
310
    $key =~ s/[\x00-\x20]/_/g;
317
    $key =~ s/[\x00-\x20]/_/g;
311
    $cache ||= 'cache';
312
    croak "No key" unless $key;
318
    croak "No key" unless $key;
313
    $ENV{DEBUG} && carp "get_from_cache for $key";
319
    $ENV{DEBUG} && carp "get_from_cache for $key";
314
    return unless ( $self->{$cache} && ref( $self->{$cache} ) =~ m/^Cache::/ );
320
    return unless ( $self->{$cache} && ref( $self->{$cache} ) =~ m/^Cache::/ );
315
321
316
    # Return L1 cache value if exists
322
    # Return L1 cache value if exists
317
    if ( exists $L1_cache{$key} ) {
323
    if ( exists $L1_cache{$key} ) {
318
        # No need to deep copy if it's a scalar:
324
        # No need to deep copy if it's a scalar
325
        # Or if we do not need to deep copy
319
        return $L1_cache{$key}
326
        return $L1_cache{$key}
320
            unless ref $L1_cache{$key};
327
            if not ref $L1_cache{$key} or $unsafe;
321
        return clone $L1_cache{$key};
328
        return clone $L1_cache{$key};
322
    }
329
    }
323
330
(-)a/t/Cache.t (-3 / +8 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 35;
20
use Test::More tests => 37;
21
21
22
my $destructorcount = 0;
22
my $destructorcount = 0;
23
23
Lines 33-39 SKIP: { Link Here
33
    $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
33
    $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
34
    my $cache = Koha::Cache->get_instance();
34
    my $cache = Koha::Cache->get_instance();
35
35
36
    skip "Cache not enabled", 31
36
    skip "Cache not enabled", 33
37
      unless ( $cache->is_cache_active() && defined $cache );
37
      unless ( $cache->is_cache_active() && defined $cache );
38
38
39
    # test fetching an item that isnt in the cache
39
    # test fetching an item that isnt in the cache
Lines 181-192 SKIP: { Link Here
181
    $item_from_cache = $cache->get_from_cache('test_deep_copy_array');
181
    $item_from_cache = $cache->get_from_cache('test_deep_copy_array');
182
    @$item_from_cache = qw( another array ref );
182
    @$item_from_cache = qw( another array ref );
183
    is_deeply( $cache->get_from_cache('test_deep_copy_array'), [ qw ( an array ref ) ], 'An array will be deep copied');
183
    is_deeply( $cache->get_from_cache('test_deep_copy_array'), [ qw ( an array ref ) ], 'An array will be deep copied');
184
    $item_from_cache = $cache->get_from_cache('test_deep_copy_array', { unsafe => 1 });
185
    @$item_from_cache = qw( another array ref );
186
    is_deeply( $cache->get_from_cache('test_deep_copy_array'), [ qw ( another array ref ) ], 'An array will not be deep copied if the unsafe flag is set');
184
    # Hash
187
    # Hash
185
    my %item = ( a => 'hashref' );
188
    my %item = ( a => 'hashref' );
186
    $cache->set_in_cache('test_deep_copy_hash', \%item);
189
    $cache->set_in_cache('test_deep_copy_hash', \%item);
187
    $item_from_cache = $cache->get_from_cache('test_deep_copy_hash');
190
    $item_from_cache = $cache->get_from_cache('test_deep_copy_hash');
188
    %$item_from_cache = ( another => 'hashref' );
191
    %$item_from_cache = ( another => 'hashref' );
189
    is_deeply( $cache->get_from_cache('test_deep_copy_hash'), { a => 'hashref' }, 'A hash will be deep copied');
192
    is_deeply( $cache->get_from_cache('test_deep_copy_hash'), { a => 'hashref' }, 'A hash will be deep copied');
193
    $item_from_cache = $cache->get_from_cache('test_deep_copy_hash', { unsafe => 1});
194
    %$item_from_cache = ( another => 'hashref' );
195
    is_deeply( $cache->get_from_cache('test_deep_copy_hash'), { another => 'hashref' }, 'A hash will not be deep copied if the unsafe flag is set');
190
}
196
}
191
197
192
END {
198
END {
193
- 

Return to bug 16044