+ is( $cache->get_from_cache("flush_me"),
+ undef, "fetching flushed item from cache" );
+ is( $cache->get_from_cache("dont_clear_me"),
+ undef, "fetching flushed item from cache" );
+
+ my $constructorcount = 0;
+ my $myscalar = $cache->create_scalar(
+ {
+ 'key' => 'myscalar',
+ 'timeout' => 1,
+ 'allowupdate' => 1,
+ 'unset' => 1,
+ 'constructor' => sub { return ++$constructorcount; },
+ 'destructor' => sub { return ++$destructorcount; },
+ }
+ );
+ ok( defined($myscalar), 'Created tied scalar' );
+ is( $$myscalar, 1, 'Constructor called to first initialize' );
+ is( $$myscalar, 1, 'Data retrieved from cache' );
+ sleep 2;
+ is( $$myscalar, 2, 'Constructor called again when timeout reached' );
+ $$myscalar = 5;
+ is( $$myscalar, 5, 'Stored new value to cache' );
+ is( $constructorcount, 2, 'Constructor not called after storing value' );
+ undef $myscalar;
+
+ is( $cache->get_from_cache("myscalar"),
+ undef, 'Item removed from cache on destruction' );
+
+ my %hash = ( 'key' => 'value' );
+
+ my $myhash = $cache->create_hash(
+ {
+ 'key' => 'myhash',
+ 'timeout' => 1,
+ 'allowupdate' => 1,
+ 'unset' => 1,
+ 'constructor' => sub { return { %hash }; },
+ }
+ );
+
+ ok(defined $myhash, 'Created tied hash');
+
+ is($myhash->{'key'}, 'value', 'Found expected value in hash');
+ ok(exists $myhash->{'key'}, 'Exists works');
+ $myhash->{'key2'} = 'surprise';
+ is($myhash->{'key2'}, 'surprise', 'Setting hash member worked');
+ $hash{'key2'} = 'nosurprise';
+ sleep 2;
+ is($myhash->{'key2'}, 'nosurprise', 'Cache change caught');
+
+
+ my $foundkeys = 0;
+ foreach my $key (keys %{$myhash}) {
+ $foundkeys++;
+ }
+
+ is($foundkeys, 2, 'Found expected 2 keys when iterating through hash');
+
+ isnt(scalar %{$myhash}, undef, 'scalar knows the hash is not empty');
+
+ $hash{'anotherkey'} = 'anothervalue';
+
+ sleep 2;
+
+ ok(exists $myhash->{'anotherkey'}, 'Cache reset properly');
+
+ delete $hash{'anotherkey'};
+ delete $myhash->{'anotherkey'};
+
+ ok(!exists $myhash->{'anotherkey'}, 'Key successfully deleted');
+
+ undef %hash;
+ %{$myhash} = ();
+
+ is(scalar %{$myhash}, 0, 'hash cleared');
+
+ $hash{'key'} = 'value';
+ is($myhash->{'key'}, 'value', 'retrieved value after clearing cache');
+}
+
+END {
+ SKIP: {
+ skip "Cache not enabled", 1
+ unless ( Koha::Cache->is_cache_active() );
+ is( $destructorcount, 1, 'Destructor run exactly once' );
+ }