From ace0e0738947894f5065b0ca1a7bacf002759e0f Mon Sep 17 00:00:00 2001 From: Masahiro Chiba Date: Sat, 6 Oct 2012 01:16:00 +0900 Subject: [PATCH] support expires in Store::Cache --- lib/Plack/Session/Store/Cache.pm | 13 +++++-- t/005_basic_w_cache_store.t | 63 +++++++++++++++++++++++++++++++- t/005a_basic_w_cache_store.t | 62 ++++++++++++++++++++++++++++++- 3 files changed, 133 insertions(+), 5 deletions(-) diff --git a/lib/Plack/Session/Store/Cache.pm b/lib/Plack/Session/Store/Cache.pm index cdfd4d4..410d33f 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 expires ]; sub new { my ($class, %params) = @_; @@ -30,7 +30,7 @@ sub fetch { sub store { my ($self, $session_id, $session) = @_; - $self->cache->set($session_id => $session); + $self->cache->set($session_id => $session, $self->expires); } sub remove { @@ -61,7 +61,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'), + expires => 86400, ); $app; }; @@ -89,6 +90,12 @@ exception if that is not the case. A simple accessor for the cache handle. +=item B + +This value uses in set method, Like this + + $cache->set($key, $data, $expires) + =back =head1 BUGS diff --git a/t/005_basic_w_cache_store.t b/t/005_basic_w_cache_store.t index 3cb9b24..64f3a37 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,32 @@ use t::lib::TestSession; delete $self->{$key}; } } +{ + package TestCacheMatchExpires; + use base 'TestCache'; + + sub set { + my ($self, $key, $val, $expires ) = @_; + + Test::More::is $self->{expires} => $expires; + + $self->{$key} = $val; + } + +} +{ + package TestCacheDenyExpires; + use base 'TestCache'; + + sub set { + my ($self, $key, $val, $expires ) = @_; + + Test::More::is $expires => undef; + + $self->{$key} = $val; + } + +} t::lib::TestSession::run_all_tests( store => Plack::Session::Store::Cache->new( cache => TestCache->new ), @@ -55,5 +82,39 @@ t::lib::TestSession::run_all_tests( }, ); +t::lib::TestSession::run_all_tests( + store => Plack::Session::Store::Cache->new( cache => TestCacheMatchExpires->new(expires => 111), expires => 111 ), + 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] || +{}}, + }; + }, +); + +t::lib::TestSession::run_all_tests( + store => Plack::Session::Store::Cache->new( cache => TestCacheDenyExpires->new ), + 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..d2c2cf1 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,32 @@ use t::lib::TestSessionHash; delete $self->{$key}; } } +{ + package TestCacheMatchExpires; + use base 'TestCache'; + + sub set { + my ($self, $key, $val, $expires ) = @_; + + Test::More::is $self->{expires} => $expires; + + $self->{$key} = $val; + } + +} +{ + package TestCacheDenyExpires; + use base 'TestCache'; + + sub set { + my ($self, $key, $val, $expires ) = @_; + + Test::More::is $expires => undef; + + $self->{$key} = $val; + } + +} t::lib::TestSessionHash::run_all_tests( store => Plack::Session::Store::Cache->new( cache => TestCache->new ), @@ -54,5 +81,38 @@ t::lib::TestSessionHash::run_all_tests( }, ); +t::lib::TestSessionHash::run_all_tests( + store => Plack::Session::Store::Cache->new( cache => TestCacheMatchExpires->new(expires => 111), expires => 111 ), + 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] || +{}}, + }; + }, +); + +t::lib::TestSessionHash::run_all_tests( + store => Plack::Session::Store::Cache->new( cache => TestCacheDenyExpires->new ), + 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;