From 9e7c4d9f994d3748fbfa9b991f5102d91656ceab Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Wed, 9 Apr 2014 14:45:20 +1200 Subject: [PATCH] Bug 12041 - utf8 compatibility changes Switched to Cache::Memcached::Fast due to its better support for unicode, added to the test case so that we notice if something causes it to break. --- Koha/Cache.pm | 11 +++++++---- t/Cache.t | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Koha/Cache.pm b/Koha/Cache.pm index 905c24e..c29d416 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -81,7 +81,7 @@ sub new { $self->{'timeout'} ||= 0; $self->{'namespace'} ||= $ENV{MEMCACHED_NAMESPACE} || 'koha'; - if ( can_load( modules => { 'Cache::Memcached' => undef } ) ) { + if ( can_load( modules => { 'Cache::Memcached::Fast' => undef } ) ) { _initialize_memcached($self); if ( $self->{'default_type'} eq 'memcached' && defined( $self->{'memcached_cache'} ) ) @@ -140,13 +140,14 @@ sub _initialize_memcached { . " with " . $self->{'namespace'}; # Cache::Memcached::Fast doesn't allow a default expire time to be set - my $memcached = Cache::Memcached->new( + # so we force it on setting. + my $memcached = Cache::Memcached::Fast->new( { servers => \@servers, compress_threshold => 10_000, namespace => $self->{'namespace'}, - expire_time => 600, debug => 0, + utf8 => 1, } ); # Ensure we can actually talk to the memcached server @@ -255,8 +256,10 @@ sub set_in_cache { return unless ( $self->{$cache} && ref( $self->{$cache} ) =~ m/^Cache::/ ); my $expiry = $options->{expiry}; + $expiry //= $self->{timeout}; my $set_sub = $self->{ref($self->{$cache}) . "_set"}; - if ( defined $expiry ) { + # We consider an expiry of 0 to be inifinite + if ( $expiry ) { return $set_sub ? $set_sub->( $key, $value, $expiry ) : $self->{$cache}->set( $key, $value, $expiry ); diff --git a/t/Cache.t b/t/Cache.t index 4a4ec58..65cf52b 100644 --- a/t/Cache.t +++ b/t/Cache.t @@ -5,7 +5,7 @@ use strict; use warnings; -use Test::More tests => 29; +use Test::More tests => 32; my $destructorcount = 0; @@ -18,7 +18,7 @@ BEGIN { SKIP: { my $cache = Koha::Cache->get_instance(); - skip "Cache not enabled", 25 + skip "Cache not enabled", 28 unless ( $cache->is_cache_active() && defined $cache ); # test fetching an item that isnt in the cache @@ -134,6 +134,19 @@ SKIP: { $hash{'key'} = 'value'; is($myhash->{'key'}, 'value', 'retrieved value after clearing cache'); + + # UTF8 testing + my $utf8_str = "A Møøse once bit my sister"; + $cache->set_in_cache('utf8_1', $utf8_str); + is($cache->get_from_cache('utf8_1'), $utf8_str, 'Simple 8-bit UTF8 correctly retrieved'); + $utf8_str = "\x{20ac}"; # € + $cache->set_in_cache('utf8_1', $utf8_str); + my $utf8_res = $cache->get_from_cache('utf8_1'); + # This'll ensure that we're getting a unicode string back, rather than + # a couple of bytes. + is(length($utf8_res), 1, 'UTF8 string length correct'); + # ...and that it's really the character we intend + is(ord($utf8_res), 8364, 'UTF8 string value correct'); } END { -- 1.8.3.2