| 1 | package Catalyst::Plugin::Session::Store::TestMemcached; | ||
| 2 | |||
| 3 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Test::Memcached; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | extends 'Catalyst::Plugin::Session::Store::Memcached'; | ||
| 8 | |||
| 9 | our $VERSION = '0.001'; | ||
| 10 | |||
| 11 | (my $memd = Test::Memcached->new) | ||
| 12 | ->start; | ||
| 13 | |||
| 14 | before 'setup_session', sub { | ||
| 15 | (my $self = shift) | ||
| 16 | ->_session_plugin_config | ||
| 17 | ->{__test_memcached} = $memd; | ||
| 18 | |||
| 19 | $self->_session_plugin_config | ||
| 20 | ->{memcached_new_args} | ||
| 21 | ->{data} = "127.0.0.1:${\$memd->option('tcp_port')}"; | ||
| 22 | }; | ||
| 23 | |||
| 24 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | Catalyst::Plugin::Session::Store::TestMemcached - Automatic creation of test Memcached instance | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | In your L<Catalyst> application class: | ||
| 33 | |||
| 34 | package MyApp::Web; | ||
| 35 | |||
| 36 | our $VERSION = '0.01'; | ||
| 37 | |||
| 38 | use Moose; | ||
| 39 | use Catalyst qw/ | ||
| 40 | Session | ||
| 41 | Session::Store::TestMemcached | ||
| 42 | Session::State::Cookie | ||
| 43 | /; | ||
| 44 | |||
| 45 | extends 'Catalyst'; | ||
| 46 | |||
| 47 | __PACKAGE__->setup; | ||
| 48 | __PACKAGE__->meta->make_immutable; | ||
| 49 | |||
| 50 | Later in a controller: | ||
| 51 | |||
| 52 | package MyApp::Controller::Root; | ||
| 53 | |||
| 54 | use Moose; | ||
| 55 | use MooseX::MethodAttributes; | ||
| 56 | |||
| 57 | extends 'Catalyst::Controller'; | ||
| 58 | |||
| 59 | sub welcome : Path(welcome) { | ||
| 60 | my ($self, $ctx) = @_; | ||
| 61 | my $count = ++$ctx->session->{count}; | ||
| 62 | $ctx->session(count => $count); | ||
| 63 | $ctx->res->body("Welcome to Catalyst: $count"); | ||
| 64 | } | ||
| 65 | |||
| 66 | __PACKAGE__->meta->make_immutable; | ||
| 67 | |||
| 68 | =head1 DESCRIPTION | ||
| 69 | |||
| 70 | This uses L<Test::Memcached> to make an application scoped instance of a | ||
| 71 | memcached server, so that if you want to test using memcached as a store for | ||
| 72 | sessions you don't need to run it in a separate job. | ||
| 73 | |||
| 74 | This is probably useful only for testing and prototypes. Additionally, many | ||
| 75 | people suggest using memcached, which is not really a persistent data store, | ||
| 76 | for sessions is not a great practice. As you wish! | ||
| 77 | |||
| 78 | =head1 AUTHOR | ||
| 79 | |||
| 80 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||
| 81 | |||
| 82 | =head1 SEE ALSO | ||
| 83 | |||
| 84 | L<Catalyst::Plugin::Session::Store::Memcached>, L<Catalyst::Plugin::Session>, | ||
| 85 | L<Catalyst>, L<Test::Memcached> | ||
| 86 | |||
| 87 | =head1 COPYRIGHT & LICENSE | ||
| 88 | |||
| 89 | Copyright 2012, John Napiorkowski L<email:jjnapiork@cpan.org> | ||
| 90 | |||
| 91 | This library is free software; you can redistribute it and/or modify it under | ||
| 92 | the same terms as Perl itself. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||