summaryrefslogtreecommitdiffstats
path: root/lib/App/ArchLinux/PackagerTools/Cache.pm
blob: 6bfbd108e05e2a3a158747eab48cb015d911850a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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: $context, $deps = {}) {
	$deps->{config} //= App::ArchLinux::PackagerTools::Config->new($context);
	my $config = $deps->{config}->get_config();

	$deps->{CHI} //= CHI->new(
		driver => 'File',
		root_dir => $config->{cache}->{root_dir},
	);
	return $class->new_no_defaults($context, $deps);
}

method new_no_defaults($class: $context, $deps = {}) {
	return $context->{$class} if defined $context->{$class};

	my $self = {};
	bless $self, $class;
	$self->{deps} = $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->{deps}->{CHI}->compute($key, $timeout, $sub);
}

1;

__END__