Skip to content

Commit d28123d

Browse files
committed
[ci] save in file and redis
1 parent 3db7903 commit d28123d

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

lib/Data/Validate/Sanctions/Redis.pm

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ use warnings;
55

66
use parent 'Data::Validate::Sanctions';
77

8-
require Exporter;
9-
our @ISA = qw(Exporter);
10-
our @EXPORT_OK = qw/is_sanctioned set_sanction_file get_sanction_file/;
11-
128
use Data::Validate::Sanctions::Fetcher;
139
use Scalar::Util qw(blessed);
1410
use List::Util qw(max);
1511
use JSON::MaybeUTF8 qw(encode_json_utf8 decode_json_utf8);
1612

13+
require Exporter;
14+
our @ISA = qw(Exporter);
15+
our @EXPORT_OK = qw/is_sanctioned set_sanction_file get_sanction_file/;
16+
17+
my $sanction_file = Data::Validate::Sanctions::_default_sanction_file();
18+
1719
# VERSION
1820

1921
my $instance;
@@ -24,7 +26,8 @@ sub new {
2426
return $instance if $instance;
2527

2628
my $self = {};
27-
$self->{redis_read} = $args{redis_read} or die 'Redis read connection is missing';
29+
$self->{sanction_file} = $args{sanction_file} // Data::Validate::Sanctions::_default_sanction_file();
30+
$self->{redis_read} = $args{redis_read} or die 'Redis read connection is missing';
2831
$self->{redis_write} = $args{redis_write} or die 'Redis write connection is missing';
2932

3033
$self->{sources} = [keys Data::Validate::Sanctions::Fetcher::config(eu_token => 'dummy')->%*];
@@ -47,14 +50,6 @@ sub last_updated {
4750
}
4851
}
4952

50-
sub set_sanction_file {
51-
die 'Not applicable';
52-
}
53-
54-
sub get_sanction_file {
55-
die 'Not applicable';
56-
}
57-
5853
sub get_sanctioned_info {
5954
my $self = blessed($_[0]) ? shift : $instance;
6055

@@ -64,31 +59,31 @@ sub get_sanctioned_info {
6459
}
6560

6661
sub _load_data {
67-
my $self = shift;
68-
69-
$self->{last_time} //= 0;
70-
$self->{_data} //= {};
71-
$self->{_sanctioned_name_tokens} //= {};
72-
$self->{_token_sanctioned_names} //= {};
73-
74-
my $last_time;
62+
my $self = shift;
63+
64+
$self->{last_time} //= 0;
65+
$self->{_data} //= {};
66+
$self->{_sanctioned_name_tokens} //= {};
67+
$self->{_token_sanctioned_names} //= {};
68+
69+
my $last_time = $self->{last_time};
7570
for my $source ($self->{sources}->@*) {
76-
my $updated = $self->{redis_read}->hget("SANCTIONS::$source", 'updated') // 0;
71+
my $updated = $self->{redis_read}->hget("SANCTIONS::$source", 'published') // 0;
7772
next if $updated <= $self->{last_time};
7873

7974
$self->{_data}->{$source}->{content} = decode_json_utf8($self->{redis_read}->hget("SANCTIONS::$source", 'content'));
8075
$self->{_data}->{$source}->{updated} = $updated;
81-
$last_time = $updated if $updated > $last_time;
76+
$last_time = $updated if $updated > $last_time;
8277
}
83-
$self->{_last_time} = $last_time;
78+
$self->{last_time} = $last_time;
8479

8580
$self->_index_data();
8681

8782
foreach my $sanctioned_name (keys $self->{_index}->%*) {
88-
my @tokens = _clean_names($sanctioned_name);
83+
my @tokens = Data::Validate::Sanctions::_clean_names($sanctioned_name);
8984
$self->{_sanctioned_name_tokens}->{$sanctioned_name} = \@tokens;
90-
foreach my $token (@tokens){
91-
$self->{_token_sanctioned_names}->{$token}->{$sanctioned_name}=1;
85+
foreach my $token (@tokens) {
86+
$self->{_token_sanctioned_names}->{$token}->{$sanctioned_name} = 1;
9287
}
9388
}
9489

@@ -98,24 +93,21 @@ sub _load_data {
9893
sub _save_data {
9994
my $self = shift;
10095

101-
my $now = time;
96+
Data::Validate::Sanctions::_save_data($self);
97+
10298
for my $source ($self->{sources}->@*) {
10399
$self->{redis_write}->hmset(
104-
"SANCTIONS::$source",
105-
'updated', $self->{_data}->{$source}->{updated},
106-
'content', encode_json_utf8($self->{_data}->{$source}->{content}),
107-
'fetched', $now,
108-
'error', $self->{_data}->{$source}->{error}
100+
"SANCTIONS::$source",
101+
'published' => $self->{_data}->{$source}->{updated},
102+
'content' => encode_json_utf8($self->{_data}->{$source}->{content}),
103+
$self->{_data}->{$source}->{error}? () : ('fetched' => $self->{last_time}),
104+
'error' => $self->{_data}->{$source}->{error},
109105
);
110106
}
111107

112108
return;
113109
}
114110

115-
sub _default_sanction_file {
116-
die 'Not applicable';
117-
}
118-
119111
1;
120112
__END__
121113

0 commit comments

Comments
 (0)