diff options
author | Florian Pritz <bluewind@xinu.at> | 2018-12-17 16:20:20 +0100 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2018-12-17 16:20:20 +0100 |
commit | 20e07437f413e338f2b54447133e1533f5262c13 (patch) | |
tree | c76b32b67ada1fdda9d52314a1f7445ebf9761eb /lib/App/ArchLinux/PackagerTools/Cache.pm | |
parent | a64d07890544d2d2a503c52a6d92ff3e064c5585 (diff) | |
download | App-ArchLinux-PackagerTools-20e07437f413e338f2b54447133e1533f5262c13.tar.gz App-ArchLinux-PackagerTools-20e07437f413e338f2b54447133e1533f5262c13.tar.xz |
WIP Add cache
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'lib/App/ArchLinux/PackagerTools/Cache.pm')
-rw-r--r-- | lib/App/ArchLinux/PackagerTools/Cache.pm | 77 |
1 files changed, 77 insertions, 0 deletions
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__ |