line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CHI::Driver::MySQL;
|
2
|
1
|
|
|
1
|
|
16381
|
use Mojo::Base 'CHI::Driver';
|
|
1
|
|
|
|
|
8405
|
|
|
1
|
|
|
|
|
5
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.1';
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Encode qw(encode);
|
7
|
|
|
|
|
|
|
use Mojo::mysql;
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has 'namespace' => sub { 'default' };
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'dsn' => sub {
|
12
|
|
|
|
|
|
|
return shift->constructor_params->{ dsn };
|
13
|
|
|
|
|
|
|
};
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has 'mysql' => sub {
|
16
|
|
|
|
|
|
|
my $self = shift;
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $mysql = Mojo::mysql->new( $self->dsn );
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
$mysql->max_connections(1);
|
21
|
|
|
|
|
|
|
$mysql->migrations->name('chi_cache')->from_data;
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$mysql->once(connection => sub { shift->migrations->migrate });
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
return $mysql;
|
26
|
|
|
|
|
|
|
};
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub clear {
|
29
|
|
|
|
|
|
|
my $self = shift;
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $sql = 'delete from `chi_cache` where `namespace` = ?';
|
32
|
|
|
|
|
|
|
$self->mysql->db->query($sql, $self->namespace);
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
return 1;
|
35
|
|
|
|
|
|
|
}
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub fetch {
|
38
|
|
|
|
|
|
|
my ( $self, $key ) = @_;
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $sql = 'select `value` from `chi_cache` where `namespace` = ? and `key` = ?';
|
41
|
|
|
|
|
|
|
my $result = $self->mysql->db->query($sql, $self->namespace, $key)
|
42
|
|
|
|
|
|
|
->hash;
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
return $result && $result->{ value };
|
45
|
|
|
|
|
|
|
}
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub get_keys {
|
48
|
|
|
|
|
|
|
my $self = shift;
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $sql = 'select `key` from `chi_cache` where `namespace` = ?';
|
51
|
|
|
|
|
|
|
my $keys = $self->mysql->db->query($sql, $self->namespace)
|
52
|
|
|
|
|
|
|
->arrays->map(sub { $_->[0] })->to_array;
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
return @$keys;
|
55
|
|
|
|
|
|
|
}
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub get_namespaces {
|
58
|
|
|
|
|
|
|
my $self = shift;
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $sql = 'select distinct(`namespace`) from `chi_cache`';
|
61
|
|
|
|
|
|
|
my $namespaces = $self->mysql->db->query($sql)
|
62
|
|
|
|
|
|
|
->arrays->map(sub { $_->[0] })->to_array;
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
return @$namespaces;
|
65
|
|
|
|
|
|
|
}
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub remove {
|
68
|
|
|
|
|
|
|
my ( $self, $key ) = @_;
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $sql = 'delete from `chi_cache` where `namespace` = ? and `key` = ?';
|
71
|
|
|
|
|
|
|
$self->mysql->db->query($sql, $self->namespace, $key);
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
return 1;
|
74
|
|
|
|
|
|
|
}
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub store {
|
77
|
|
|
|
|
|
|
my ( $self, $key, $data ) = @_;
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
if ( $self->get($key) ) {
|
80
|
|
|
|
|
|
|
my $sql = 'update `chi_cache` set `value` = ?, `updated_at` = now() where `namespace` = ? and `key` = ?';
|
81
|
|
|
|
|
|
|
$self->mysql->db->query($sql, encode('UTF-8', $data), $self->namespace, $key);
|
82
|
|
|
|
|
|
|
} else {
|
83
|
|
|
|
|
|
|
my $sql = 'insert into `chi_cache` (`namespace`, `key`, `value`, `created_at`) values (?, ?, ?, now())';
|
84
|
|
|
|
|
|
|
$self->mysql->db->query($sql, $self->namespace, $key, encode('UTF-8', $data));
|
85
|
|
|
|
|
|
|
}
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
return 1;
|
88
|
|
|
|
|
|
|
}
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1;
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
__END__
|