Cache::Curried
[catagits/Catalyst-Plugin-Cache.git] / t / key_regexes.t
CommitLineData
c28ee69c 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More 'no_plan';
7
8use ok "Catalyst::Plugin::Cache";
9use 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
35MockApp->setup;
36my $c = bless {}, "MockApp";
37
38MockApp->register_cache_backend( default => MemoryCache->new );
39MockApp->register_cache_backend( foo_store => MemoryCache->new );
40MockApp->register_cache_backend( bar_store => MemoryCache->new );
41
2e4bde89 42is( $c->choose_cache_backend_wrapper( key => "baz" ), $c->default_cache_backend, "chose default" );
43is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->get_cache_backend("foo_store"), "chose foo" );
44is( $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 50is( $c->default_cache_backend->get("baz_laa"), "laa", "non match stored in default" );
51is( $c->default_cache_backend->get("foo_laa"), undef, "no foo key" );
52is( $c->default_cache_backend->get("bar_laa"), undef, "no bar key" );
c28ee69c 53
54
2e4bde89 55is( $c->get_cache_backend("foo_store")->get("baz_laa"), undef, "no non match in foo store" );
56is( $c->get_cache_backend("foo_store")->get("foo_laa"), "laa", "has foo key" );
57is( $c->get_cache_backend("foo_store")->get("bar_laa"), undef, "no bar key" );
c28ee69c 58
59
2e4bde89 60is( $c->get_cache_backend("bar_store")->get("baz_laa"), undef, "no non match in bar store" );
61is( $c->get_cache_backend("bar_store")->get("foo_laa"), undef, "no foo key" );
62is( $c->get_cache_backend("bar_store")->get("bar_laa"), "laa", "has bar key" );
c28ee69c 63
64