Skip Menu |

This queue is for tickets about the App-Cache CPAN distribution.

Report information
The Basics
Id: 46765
Status: resolved
Priority: 0/
Queue: App-Cache

People
Owner: Nobody in particular
Requestors: sysmonblog [...] googlemail.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: $app_cache->directory
Not a bug per-se, but a request for details prior to possibly submitting a bug & patch. In order to clean up the cache directory after myself in the tests of a module I'm working on that uses App::Cache [1], I'd like the "directory" method in App::Cache to be public. It's currently an un-documented feature, thus I'm (being paranoid) about wondering if it's safe to use. I'm wondering what your preference would be, if any? (a) patch that adds pod for it (b) pod patch and test - I'm not clear what it would actually test, other than that a directory method existed? (c) something else? If you are so willing, would a patch to the Makefile.PL [1] to clean up the .app_cache_test directory after running the tests be welcome also? [1] http://www.mail-archive.com/module-authors@perl.org/msg07343.html
This seems sensible. I'd accept those patches ;-) Leon
RT-Send-CC: sysmonblog [...] googlemail.com
Attached are three (git) patches. First simply adds some pod to document the $cache->directory method. Second attempts to remove the cache directory created by running the tests, such that there should be no lingering directories after the tests have run. The third is a little more extensive and it does what I would like, but you might not. It allows the directory to be set by the caller. Tests are included. Yours, sysmon (aka mintywalker)
From 4d69f34e77f440249f5f4c52ee67aaba9da10b49 Mon Sep 17 00:00:00 2001 From: Murray <murray@minty.org> Date: Thu, 25 Jun 2009 23:32:19 +0100 Subject: [PATCH] Add POD for directory method --- lib/App/Cache.pm | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-) diff --git a/lib/App/Cache.pm b/lib/App/Cache.pm index 1f63f5d..f754149 100644 --- a/lib/App/Cache.pm +++ b/lib/App/Cache.pm @@ -235,6 +235,15 @@ Perl data structure: $cache->set('test', 'one'); $cache->set('test', { foo => 'bar' }); +=head2 directory + +Returns the full path to the cache directory. Primarily useful for when you +are writing tests that use App::Cache and want to clean up after yourself. If +you are doing that you may want to explicitly set the 'application' constructor +parameter to avoid later cleaning up a cache dir that was already in use. + + my $dir = $cache->directory; + =head1 AUTHOR Leon Brocard <acme@astray.com> -- 1.6.0.4
From 43bf18aa5c367e5cc0ce168d2e51e7527708e041 Mon Sep 17 00:00:00 2001 From: Murray <murray@minty.org> Date: Fri, 26 Jun 2009 00:09:58 +0100 Subject: [PATCH] Allow cache directory to be set by caller. --- lib/App/Cache.pm | 52 ++++++++++++++++++++++++++++++++-------------- t/lib/App/Cache/Test.pm | 19 +++++++++++++++++ t/simple.t | 3 +- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/lib/App/Cache.pm b/lib/App/Cache.pm index f754149..77bbfcb 100644 --- a/lib/App/Cache.pm +++ b/lib/App/Cache.pm @@ -2,6 +2,7 @@ package App::Cache; use strict; use File::Find::Rule; use File::HomeDir; +use File::Path qw( mkpath ); use File::stat; use HTTP::Cookies; use LWP::UserAgent; @@ -20,16 +21,14 @@ sub new { $self->application($caller); } - my $directory = dir(home(), "." . $self->_clean($self->application), "cache"); - $self->directory($directory); - - my $topdirectory = dir(home(), "." . $self->_clean($self->application)); - unless (-d $topdirectory) { - mkdir($topdirectory) || die "Error mkdiring $topdirectory: $!"; + unless ($self->directory) { + my $dir = dir(home(), "." . $self->_clean($self->application), "cache"); + $self->directory($dir); } - - unless (-d $directory) { - mkdir($directory) || die "Error mkdiring $directory: $!"; + my $dir = $self->directory; + unless (-d "$dir") { + mkpath("$dir") + || die "Error mkdiring " . $self->directory . ": $!"; } return $self; @@ -176,14 +175,35 @@ per-application cache. =head2 new -The constructor creates an L<App::Cache> object. It takes two optional -parameters: a ttl parameter which contains the number of seconds in -which a cache entry expires, and an application parameter which -signifies the application name. If you are calling new() from a class, -the application is automagically set to the calling class, so you should -rarely need to pass it in: +The constructor creates an L<App::Cache> object. It takes three optional +parameters: - my $cache = App::Cache->new({ ttl => 60*60 }); +=over + +=item * + +ttl contains the number of seconds in which a cache entry expires. The default +is 30 minutes. + + my $cache = App::Cache->new({ ttl => 30*60 }); + +=item * + +application sets the application name. If you are calling new() from a class, +the application is automagically set to the calling class, so you should rarely +need to pass it in: + + my $cache = App::Cache->new({ application => 'Your::Module' }); + +=item * + +directory sets the directory to be used for the cache. Normally this is just +set for you and will be based on the application name and be created in the +users home directory. Sometimes for testing, it can be useful to set this. + + my $cache = App::Cache->new({ directory => '/tmp/your/cache/dir' }); + +=back =head2 clear diff --git a/t/lib/App/Cache/Test.pm b/t/lib/App/Cache/Test.pm index 4bf32f3..ed23d75 100644 --- a/t/lib/App/Cache/Test.pm +++ b/t/lib/App/Cache/Test.pm @@ -7,6 +7,8 @@ use Path::Class qw(); use Storable qw(nstore retrieve); use File::Path qw(rmtree); use Test::More; +use File::Temp qw(tempdir); +use File::Path qw(mkpath rmtree); use base qw( Class::Accessor::Chained::Fast ); __PACKAGE__->mk_accessors(qw()); @@ -103,5 +105,22 @@ sub scratch { } } +sub dir { + my $self = shift; + my $tmp_dir = tempdir( CLEANUP => 1 ); + $self->with_dir($tmp_dir); + rmtree( $tmp_dir ); + ok(!-d $tmp_dir, 'tmp_dir removed successfully'); + $self->with_dir($tmp_dir); +} + +sub with_dir { + my ($self, $dir) = @_; + my $cache = App::Cache->new({ directory => $dir }); + isa_ok( $cache, 'App::Cache' ); + is( $cache->directory, $dir ); + ok( -d $dir, 'tmp_dir exists ok' ); +} + 1; diff --git a/t/simple.t b/t/simple.t index 75f0185..577bd35 100644 --- a/t/simple.t +++ b/t/simple.t @@ -1,7 +1,7 @@ #!perl use strict; use lib qw(lib t/lib); -use Test::More tests => 40; +use Test::More tests => 47; use File::Spec::Functions qw(rel2abs); use_ok('App::Cache'); use_ok('App::Cache::Test'); @@ -9,6 +9,7 @@ use_ok('App::Cache::Test'); my $cache = App::Cache::Test->new(); $cache->code; $cache->file; +$cache->dir; $cache->scratch; $cache->url( 'file:/' . rel2abs( $INC{'App/Cache/Test.pm'} ) ); $cache->url('http://www.astray.com/'); -- 1.6.0.4
From cce65881f5dd5d30095774290c1bbc46b2016278 Mon Sep 17 00:00:00 2001 From: Murray <murray@minty.org> Date: Fri, 26 Jun 2009 00:09:30 +0100 Subject: [PATCH] Delete App::Cache::Test cache dir when done --- t/lib/App/Cache/Test.pm | 8 ++++++++ t/simple.t | 3 ++- 2 files changed, 10 insertions(+), 1 deletions(-) diff --git a/t/lib/App/Cache/Test.pm b/t/lib/App/Cache/Test.pm index fa49802..4bf32f3 100644 --- a/t/lib/App/Cache/Test.pm +++ b/t/lib/App/Cache/Test.pm @@ -5,10 +5,18 @@ use Digest::MD5 qw(md5 md5_hex md5_base64); use LWP::Simple qw(get); use Path::Class qw(); use Storable qw(nstore retrieve); +use File::Path qw(rmtree); use Test::More; use base qw( Class::Accessor::Chained::Fast ); __PACKAGE__->mk_accessors(qw()); +sub cleanup { + my $self = shift; + my $cache = App::Cache->new; + rmtree($cache->directory->parent->stringify ); + ok(!-d $cache->directory->parent, 'removed cache dir'); +} + sub file { my $self = shift; my $cache = App::Cache->new; diff --git a/t/simple.t b/t/simple.t index 0e87875..75f0185 100644 --- a/t/simple.t +++ b/t/simple.t @@ -1,7 +1,7 @@ #!perl use strict; use lib qw(lib t/lib); -use Test::More tests => 39; +use Test::More tests => 40; use File::Spec::Functions qw(rel2abs); use_ok('App::Cache'); use_ok('App::Cache::Test'); @@ -12,4 +12,5 @@ $cache->file; $cache->scratch; $cache->url( 'file:/' . rel2abs( $INC{'App/Cache/Test.pm'} ) ); $cache->url('http://www.astray.com/'); +$cache->cleanup; -- 1.6.0.4
Thanks, applied and released as App-Cache-0.36.tar.gz.