diff --git a/lib/Plack/Session/Store/Cache.pm b/lib/Plack/Session/Store/Cache.pm index cdfd4d4..2cf45d1 100644 --- a/lib/Plack/Session/Store/Cache.pm +++ b/lib/Plack/Session/Store/Cache.pm @@ -9,7 +9,7 @@ use Scalar::Util qw[ blessed ]; use parent 'Plack::Session::Store'; -use Plack::Util::Accessor qw[ cache ]; +use Plack::Util::Accessor qw[ cache prefix ]; sub new { my ($class, %params) = @_; @@ -20,22 +20,25 @@ sub new { && $params{cache}->can('set') && $params{cache}->can('remove'); - bless { %params } => $class; + bless { + prefix => '', + %params, + } => $class; } sub fetch { my ($self, $session_id ) = @_; - $self->cache->get($session_id); + $self->cache->get($self->prefix . $session_id); } sub store { my ($self, $session_id, $session) = @_; - $self->cache->set($session_id => $session); + $self->cache->set($self->prefix . $session_id => $session); } sub remove { my ($self, $session_id) = @_; - $self->cache->remove($session_id); + $self->cache->remove($self->prefix . $session_id); } 1; @@ -61,7 +64,8 @@ Plack::Session::Store::Cache - Cache session store builder { enable 'Session', store => Plack::Session::Store::Cache->new( - cache => CHI->new(driver => 'FastMmap') + cache => CHI->new(driver => 'FastMmap'), + prefix => 'session:', ); $app; }; @@ -89,6 +93,10 @@ exception if that is not the case. A simple accessor for the cache handle. +=item B + +The prefix associated with this cache. Defaults to "" if not explicitly set. + =back =head1 BUGS diff --git a/t/005_basic_w_cache_store.t b/t/005_basic_w_cache_store.t index 3cb9b24..543cc99 100755 --- a/t/005_basic_w_cache_store.t +++ b/t/005_basic_w_cache_store.t @@ -16,7 +16,8 @@ use t::lib::TestSession; package TestCache; sub new { - bless {} => shift; + my $class = shift; + bless +{@_} => $class; } sub set { @@ -37,6 +38,35 @@ use t::lib::TestSession; delete $self->{$key}; } } +{ + package TestCachePrefix; + use base 'TestCache'; + + + sub set { + my ($self, $key, $val ) = @_; + + Test::More::like $key, qr/^$self->{prefix}/; + + $self->{$key} = $val; + } + + sub get { + my ($self, $key ) = @_; + + Test::More::like $key, qr/^$self->{prefix}/; + + $self->{$key}; + } + + sub remove { + my ($self, $key ) = @_; + + Test::More::like $key, qr/^$self->{prefix}/; + + delete $self->{$key}; + } +} t::lib::TestSession::run_all_tests( store => Plack::Session::Store::Cache->new( cache => TestCache->new ), @@ -55,5 +85,22 @@ t::lib::TestSession::run_all_tests( }, ); +t::lib::TestSession::run_all_tests( + store => Plack::Session::Store::Cache->new( cache => TestCachePrefix->new(prefix => 'test:'), prefix => 'test:' ), + state => Plack::Session::State->new, + env_cb => sub { + open my $in, '<', \do { my $d }; + my $env = { + 'psgi.version' => [ 1, 0 ], + 'psgi.input' => $in, + 'psgi.errors' => *STDERR, + 'psgi.url_scheme' => 'http', + SERVER_PORT => 80, + REQUEST_METHOD => 'GET', + QUERY_STRING => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}}, + }; + }, +); + done_testing; diff --git a/t/005a_basic_w_cache_store.t b/t/005a_basic_w_cache_store.t index 04b9f7c..0da9667 100644 --- a/t/005a_basic_w_cache_store.t +++ b/t/005a_basic_w_cache_store.t @@ -15,7 +15,8 @@ use t::lib::TestSessionHash; package TestCache; sub new { - bless {} => shift; + my $class = shift; + bless +{@_} => $class; } sub set { @@ -36,6 +37,35 @@ use t::lib::TestSessionHash; delete $self->{$key}; } } +{ + package TestCachePrefix; + use base 'TestCache'; + + + sub set { + my ($self, $key, $val ) = @_; + + Test::More::like $key, qr/^$self->{prefix}/; + + $self->{$key} = $val; + } + + sub get { + my ($self, $key ) = @_; + + Test::More::like $key, qr/^$self->{prefix}/; + + $self->{$key}; + } + + sub remove { + my ($self, $key ) = @_; + + Test::More::like $key, qr/^$self->{prefix}/; + + delete $self->{$key}; + } +} t::lib::TestSessionHash::run_all_tests( store => Plack::Session::Store::Cache->new( cache => TestCache->new ), @@ -54,5 +84,22 @@ t::lib::TestSessionHash::run_all_tests( }, ); +t::lib::TestSessionHash::run_all_tests( + store => Plack::Session::Store::Cache->new( cache => TestCachePrefix->new(prefix => 'test:'), prefix => 'test:' ), + state => Plack::Session::State->new, + env_cb => sub { + open my $in, '<', \do { my $d }; + my $env = { + 'psgi.version' => [ 1, 0 ], + 'psgi.input' => $in, + 'psgi.errors' => *STDERR, + 'psgi.url_scheme' => 'http', + SERVER_PORT => 80, + REQUEST_METHOD => 'GET', + QUERY_STRING => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}}, + }; + }, +); + done_testing;