Commit | Line | Data |
c28ee69c |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More 'no_plan'; |
7 | |
8 | use ok "Catalyst::Plugin::Cache"; |
9 | use ok "Catalyst::Plugin::Cache::Choose::KeyRegexes"; |
10 | |
11 | { |
12 | package MockApp; |
13 | use base qw/Catalyst::Plugin::Cache Catalyst::Plugin::Cache::Choose::KeyRegexes/; |
14 | |
15 | our %config = ( |
16 | cache => { |
17 | key_regexes => [ |
18 | qr/^foo/ => "foo_store", |
19 | qr/^bar/ => "bar_store", |
20 | ], |
21 | }, |
22 | ); |
23 | sub config { \%config } |
24 | |
25 | package MemoryCache; |
26 | use Storable qw/freeze thaw/; |
27 | |
28 | sub new { bless {}, shift } |
29 | sub get { ${thaw($_[0]{$_[1]}) || return} }; |
30 | sub set { $_[0]{$_[1]} = freeze(\$_[2]) }; |
31 | sub delete { delete $_[0]{$_[1]} }; |
32 | } |
33 | |
34 | |
35 | MockApp->setup; |
36 | my $c = bless {}, "MockApp"; |
37 | |
38 | MockApp->register_cache_backend( default => MemoryCache->new ); |
39 | MockApp->register_cache_backend( foo_store => MemoryCache->new ); |
40 | MockApp->register_cache_backend( bar_store => MemoryCache->new ); |
41 | |
2e4bde89 |
42 | is( $c->choose_cache_backend_wrapper( key => "baz" ), $c->default_cache_backend, "chose default" ); |
43 | is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->get_cache_backend("foo_store"), "chose foo" ); |
44 | is( $c->choose_cache_backend_wrapper( key => "bar" ), $c->get_cache_backend("bar_store"), "chose bar" ); |
c28ee69c |
45 | |
46 | $c->cache_set( foo_laa => "laa" ); |
47 | $c->cache_set( bar_laa => "laa" ); |
48 | $c->cache_set( baz_laa => "laa" ); |
49 | |
2e4bde89 |
50 | is( $c->default_cache_backend->get("baz_laa"), "laa", "non match stored in default" ); |
51 | is( $c->default_cache_backend->get("foo_laa"), undef, "no foo key" ); |
52 | is( $c->default_cache_backend->get("bar_laa"), undef, "no bar key" ); |
c28ee69c |
53 | |
54 | |
2e4bde89 |
55 | is( $c->get_cache_backend("foo_store")->get("baz_laa"), undef, "no non match in foo store" ); |
56 | is( $c->get_cache_backend("foo_store")->get("foo_laa"), "laa", "has foo key" ); |
57 | is( $c->get_cache_backend("foo_store")->get("bar_laa"), undef, "no bar key" ); |
c28ee69c |
58 | |
59 | |
2e4bde89 |
60 | is( $c->get_cache_backend("bar_store")->get("baz_laa"), undef, "no non match in bar store" ); |
61 | is( $c->get_cache_backend("bar_store")->get("foo_laa"), undef, "no foo key" ); |
62 | is( $c->get_cache_backend("bar_store")->get("bar_laa"), "laa", "has bar key" ); |
c28ee69c |
63 | |
64 | |