]> iEval git - gruntmaster-page.git/blame_incremental - lib/Gruntmaster/Data.pm
Add Account page
[gruntmaster-page.git] / lib / Gruntmaster / Data.pm
... / ...
CommitLineData
1package Gruntmaster::Data;
2use v5.14;
3use warnings;
4use parent qw/Exporter/;
5
6use JSON qw/encode_json decode_json/;
7
8use Redis;
9
10our $contest;
11my $redis = Redis->new;
12my $pubsub = Redis->new;
13
14sub dynsub{
15 no strict 'refs';
16 *{$_[0]} = $_[1];
17}
18
19BEGIN {
20 for my $cmd (qw/multi exec smembers get hget hset sadd srem incr hmset hsetnx/) {
21 dynsub uc $cmd, sub { say "Arguments to \U$cmd: ", join ', ', @_; $redis->$cmd(@_) };
22 }
23
24 for my $cmd (qw/publish subscribe wait_for_messages/) {
25 dynsub uc $cmd, sub { say "Arguments to \U$cmd: ", join ', ', @_; $pubsub->$cmd(@_) };
26 }
27}
28
29sub cp { defined $contest ? "contest.$contest." : '' }
30
31sub multi () { MULTI }
32sub rexec () { EXEC }
33
34sub problems () { SMEMBERS cp . 'problem' }
35sub contests () { SMEMBERS cp . 'contest' }
36sub jobcard () { GET cp . 'job' }
37
38sub job_results (_) { decode_json HGET cp . "job.$_[0]", 'results' }
39sub set_job_results ($+) { HSET cp . "job.$_[0]", 'results', encode_json $_[1] }
40sub job_inmeta (_) { decode_json HGET cp . "job.$_[0]", 'inmeta' }
41sub set_job_inmeta ($+) { HSET cp . "job.$_[0]", 'inmeta', encode_json $_[1] }
42sub problem_meta (_) { decode_json HGET cp . "problem.$_[0]", 'meta' }
43sub set_problem_meta ($+) { HSET cp . "problem.$_[0]", 'meta', encode_json $_[1] }
44sub job_daemon (_) { HGET cp . "job.$_[0]", 'daemon' }
45sub set_job_daemon ($$) { HSETNX cp . "job.$_[0]", 'daemon', $_[1] };
46
47sub defhash{
48 my ($name, @keys) = @_;
49 for my $key (@keys) {
50 dynsub "${name}_$key", sub (_) { HGET cp . "$name.$_[0]", $key };
51 dynsub "set_${name}_$key", sub ($$) { HSET cp . "$name.$_[0]", $key, $_[1] };
52 }
53
54 dynsub "insert_$name", sub {
55 my ($key, %values) = @_;
56 SADD cp . $name, $key or return;
57 HMSET cp . "$name.$key", %values;
58 };
59 dynsub "remove_$name", sub (_) {
60 my $key = shift;
61 SREM cp . $name, $key;
62 };
63
64 dynsub "push_$name", sub {
65 my $nr = INCR cp . $name;
66 HMSET cp . "$name.$nr", @_;
67 $nr
68 };
69}
70
71defhash problem => qw/name level statement/;
72defhash contest => qw/start end name owner/;
73defhash job => qw/date extension filesize private problem result result_text user/;
74defhash user => qw/name email town university/;
75
76our @EXPORT_OK = do {
77 no strict 'refs';
78 grep { $_ =~ /^[a-zA-Z]/ and exists &$_ } keys %{__PACKAGE__ . '::'};
79};
80
811
This page took 0.023852 seconds and 4 git commands to generate.