From 20e07437f413e338f2b54447133e1533f5262c13 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 17 Dec 2018 16:20:20 +0100 Subject: WIP Add cache Signed-off-by: Florian Pritz --- lib/App/ArchLinux/PackagerTools/CPAN.pm | 4 +- .../PackagerTools/CPAN/PackagesDetailsFetcher.pm | 3 + lib/App/ArchLinux/PackagerTools/Cache.pm | 77 ++++++++++++++++++++++ lib/App/ArchLinux/PackagerTools/Config.pm | 7 ++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 lib/App/ArchLinux/PackagerTools/Cache.pm (limited to 'lib') diff --git a/lib/App/ArchLinux/PackagerTools/CPAN.pm b/lib/App/ArchLinux/PackagerTools/CPAN.pm index 2a2e7a2..de2a397 100644 --- a/lib/App/ArchLinux/PackagerTools/CPAN.pm +++ b/lib/App/ArchLinux/PackagerTools/CPAN.pm @@ -43,14 +43,14 @@ method new($class: $deps = {}) { } method new_no_defaults($class: $deps = {}) { - return $deps->{context}->{__PACKAGE__} if defined $deps->{context}->{__PACKAGE__}; + return $deps->{context}->{$class} if defined $deps->{context}->{$class}; my $self = {}; bless $self, $class; $self->{deps} = $deps; $self->{dist_lc_map} = {}; $self->{dists} = {}; - $deps->{context}->{__PACKAGE__} = $self; + $deps->{context}->{$class} = $self; $log->debug("Initialized new CPAN object"); return $self; } diff --git a/lib/App/ArchLinux/PackagerTools/CPAN/PackagesDetailsFetcher.pm b/lib/App/ArchLinux/PackagerTools/CPAN/PackagesDetailsFetcher.pm index eb89dd6..734669c 100644 --- a/lib/App/ArchLinux/PackagerTools/CPAN/PackagesDetailsFetcher.pm +++ b/lib/App/ArchLinux/PackagerTools/CPAN/PackagesDetailsFetcher.pm @@ -6,6 +6,8 @@ use Function::Parameters; use Log::Any qw($log); use IPC::Run qw(run); +use App::ArchLinux::PackagerTools::Cache; + =head1 NAME App::ArchLinux::PackagerTools::CPAN::PackagesDetailsFetcher - Provide access to the 02packages.details CPAN file @@ -29,6 +31,7 @@ Returns a new instance. =cut method new($class: $deps = {}) { + $deps->{cache} //= App::ArchLinux::PackagerTools::Cache->new({$deps->%{context}}); return $class->new_no_defaults($deps); } diff --git a/lib/App/ArchLinux/PackagerTools/Cache.pm b/lib/App/ArchLinux/PackagerTools/Cache.pm new file mode 100644 index 0000000..b8415ad --- /dev/null +++ b/lib/App/ArchLinux/PackagerTools/Cache.pm @@ -0,0 +1,77 @@ +package App::ArchLinux::PackagerTools::Cache; +use strictures; + +use autodie; +use Function::Parameters; +use Log::Any qw($log); +use CHI; + +use App::ArchLinux::PackagerTools::Config; + +=head1 NAME + +App::ArchLinux::PackagerTools::Cache - Caching functions + +=head1 SYNOPSIS + +use App::ArchLinux::PackagerTools::Cache; + + +=head1 DESCRIPTION + +# longer description... + +=head1 METHODS + +=head2 new + + my $cache = App::ArchLinux::PackagerTools::Cache->new(); + +Returns a new instance. + +=cut + +method new($class: $deps = {}) { + $deps->{config} //= App::ArchLinux::PackagerTools::Config->new({$deps->%{context}}); + return $class->new_no_defaults($deps); +} + +method new_no_defaults($class: $deps = {}) { + return $deps->{context}->{$class} if defined $deps->{context}->{$class}; + + my $self = {}; + bless $self, $class; + $self->{deps} = $deps; + + my $config = $deps->{config}->get_config(); + + $self->{CHI} = CHI->new( + driver => 'File', + root_dir => $config->{cache}->{root_dir}, + cache_size => $config->{cache}->{cache_size}, + ); + $deps->{context}->{$class} = $self; + return $self; +} + +=head2 Public Methods + +=cut + +=head3 compute + + my $value = $cache->compute($key, $timeout, $sub); + +If a value exists for the cache key $key, it is returned. Otherwise, $sub is +executed to compute the value and it is stored in the cache with the specified +timeout. + +=cut + +method compute($key, $timeout, $sub) { + return $self->{CHI}->compute($key, $timeout, $sub); +} + +1; + +__END__ diff --git a/lib/App/ArchLinux/PackagerTools/Config.pm b/lib/App/ArchLinux/PackagerTools/Config.pm index a44c845..470a1ae 100644 --- a/lib/App/ArchLinux/PackagerTools/Config.pm +++ b/lib/App/ArchLinux/PackagerTools/Config.pm @@ -37,9 +37,12 @@ method new($class: $deps = {}) { } method new_no_defaults($class: $deps = {}) { + return $deps->{context}->{$class} if defined $deps->{context}->{$class}; + my $self = {}; bless $self, $class; $self->{deps} = $deps; + $deps->{context}->{$class} = $self; return $self; } @@ -80,6 +83,10 @@ Return a hash with the default config values. method _get_default_config() { return { + cache => { + root_dir => path(($ENV{XDG_CACHE_HOME} // $ENV{HOME}."/.cache")."/perlpkg/v1/")->stringify(), + cache_size => '50M', + }, }; } -- cgit v1.2.3-24-g4f1b