1
2#########################
3
4use Test::More tests => 9;
5BEGIN { use_ok('Cache::FastMmap') };
6use Data::Dumper;
7use strict;
8
9#########################
10
11# Test writeback and cache_not_found option
12
13# Test a backing store just made of a local hash
14my %BackingStore = (
15  foo => { key1 => '123abc' },
16  bar => undef
17);
18
19my %OrigBackingStore = %BackingStore;
20
21my $RCBCalled = 0;
22
23my $FC = Cache::FastMmap->new(
24  cache_not_found => 1,
25  init_file => 1,
26  num_pages => 89,
27  page_size => 1024,
28  context => \%BackingStore,
29  read_cb => sub { $RCBCalled++; return $_[0]->{$_[1]}; },
30  write_cb => sub { $_[0]->{$_[1]} = $_[2]; },
31  delete_cb => sub { delete $_[0]->{$_[1]} },
32  write_action => 'write_back'
33);
34
35ok( defined $FC );
36
37# Should pull from the backing store
38ok( eq_hash( $FC->get('foo'), { key1 => '123abc' } ),  "cb get 1");
39is( $FC->get('bar'), undef,  "cb get 2");
40is( $RCBCalled, 2,  "cb get 2");
41
42# Should be in the cache now
43ok( eq_hash( $FC->get('foo'), { key1 => '123abc' } ),  "cb get 3");
44is( $FC->get('bar'), undef,  "cb get 4");
45is( $RCBCalled, 2,  "cb get 2");
46
47# Need to make them dirty
48$FC->set('foo', { key1 => '123abc' });
49$FC->set('bar', undef);
50
51# Should force cache data back to backing store
52%BackingStore = ();
53$FC->empty();
54
55ok( eq_hash(\%BackingStore, \%OrigBackingStore), "items match 1" . Dumper(\%BackingStore, \%OrigBackingStore));
56
57