line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::orMapper::Memcached; |
2
|
1
|
|
|
1
|
|
32820
|
use strict; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
60
|
|
3
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
4
|
1
|
|
|
1
|
|
1214
|
use String::CRC32; |
|
1
|
|
|
|
|
870
|
|
|
1
|
|
|
|
|
87
|
|
5
|
1
|
|
|
1
|
|
1721
|
use Cache::Memcached::Fast; |
|
1
|
|
|
|
|
6283
|
|
|
1
|
|
|
|
|
31
|
|
6
|
|
|
|
|
|
|
#use Data::Dumper; |
7
|
1
|
|
|
1
|
|
6
|
use base qw/Class::orMapper/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
909
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Class::orMapper::Memcached - DBI base easy O/R Mapper with Memcached. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use Class::orMapper::Memcached; |
18
|
|
|
|
|
|
|
my $read_database = { |
19
|
|
|
|
|
|
|
dsn => 'dbi:xxxx:dbname=xxxx;host=localhost;port=xxxx', |
20
|
|
|
|
|
|
|
uid => 'xxxx', |
21
|
|
|
|
|
|
|
pwd => 'xxxx', |
22
|
|
|
|
|
|
|
opt => {AutoCommit => 0}, |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
my $write_database = { |
25
|
|
|
|
|
|
|
dsn => 'dbi:xxxx:dbname=xxxx;host=localhost;port=xxxx', |
26
|
|
|
|
|
|
|
uid => 'xxxx', |
27
|
|
|
|
|
|
|
pwd => 'xxxx', |
28
|
|
|
|
|
|
|
opt => {AutoCommit => 0}, |
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
my $memcached = { |
31
|
|
|
|
|
|
|
servers => [qw/localhost:11211/], |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
my $db = new Class::orMapper::Memcached($read_database, $write_database, $memcached); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
This Module is easy database operation module with memcached. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 Usage |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $data = $db->select_n_arrayref_c($sql,$value); |
42
|
|
|
|
|
|
|
my $data = $db->select_n_hashref_c($sql,$value); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
ex.) my $sql = "select * from test where hoge=?"; |
45
|
|
|
|
|
|
|
my $value = [qw/abc/]; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $data = $db->select_arrayref_c($param); |
48
|
|
|
|
|
|
|
my $data = $db->select_hashref_c($param); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
ex.) |
51
|
|
|
|
|
|
|
$param = { |
52
|
|
|
|
|
|
|
table => 'table_name', |
53
|
|
|
|
|
|
|
columns => [aaa,bbb,ccc], |
54
|
|
|
|
|
|
|
where => [ |
55
|
|
|
|
|
|
|
{xxx => {'=' => 'value1', '>' => 'value2'}}, |
56
|
|
|
|
|
|
|
{xxx => [qw/abc def cfg/], |
57
|
|
|
|
|
|
|
], |
58
|
|
|
|
|
|
|
order => {'yyy' => 'desc', 'zzz' => 'asc'}, |
59
|
|
|
|
|
|
|
}; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 Copyright |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Kazunori Minoda (c)2012 |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub new{ |
68
|
0
|
|
|
0
|
0
|
|
my ($this,$r,$w,$m) = @_; |
69
|
0
|
|
|
|
|
|
my $self = new Class::orMapper($r,$w); |
70
|
0
|
|
|
|
|
|
$self->{mem} = new Cache::Memcached::Fast($m); |
71
|
0
|
|
|
|
|
|
$self->{expiration_time} = 7*24*60*60; # 1week |
72
|
0
|
|
|
|
|
|
return bless($self,$this); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub select_n_arrayref_c{ |
76
|
0
|
|
|
0
|
0
|
|
my ($self,$s,$v) = @_; |
77
|
0
|
|
|
|
|
|
my $key = crc32($s.join("",@{$v}.'na')); |
|
0
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
my $q = $self->{mem}->get($key); |
79
|
0
|
0
|
|
|
|
|
if(!$q){ |
80
|
0
|
|
|
|
|
|
$q = $self->select_n_arrayref($s,$v); |
81
|
0
|
|
|
|
|
|
$self->{mem}->set($key,$q,$self->{expiration_time}); |
82
|
|
|
|
|
|
|
} |
83
|
0
|
|
|
|
|
|
return $q; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub select_n_hashref_c{ |
87
|
0
|
|
|
0
|
0
|
|
my ($self,$s,$v) = @_; |
88
|
0
|
|
|
|
|
|
my $key = crc32($s.join("",@{$v}.'nh')); |
|
0
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
my $q = $self->{mem}->get($key); |
90
|
0
|
0
|
|
|
|
|
if(!$q){ |
91
|
0
|
|
|
|
|
|
$q = $self->select_n_hashref($s,$v); |
92
|
0
|
|
|
|
|
|
$self->{mem}->set($key,$q,$self->{expiration_time}); |
93
|
|
|
|
|
|
|
} |
94
|
0
|
|
|
|
|
|
return $q; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub select_arrayref_c{ |
98
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
99
|
0
|
|
|
|
|
|
my $key = crc32(Dumper($p).'a'); |
100
|
0
|
|
|
|
|
|
my $q = $self->{mem}->get($key); |
101
|
0
|
0
|
|
|
|
|
if(!$q){ |
102
|
0
|
|
|
|
|
|
$q = $self->select_arrayref($p); |
103
|
0
|
|
|
|
|
|
$self->{mem}->set($key,$q,$self->{expiration_time}); |
104
|
|
|
|
|
|
|
} |
105
|
0
|
|
|
|
|
|
return $q; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub select_hashref_c{ |
109
|
0
|
|
|
0
|
0
|
|
my ($self,$p) = @_; |
110
|
0
|
|
|
|
|
|
my $key = crc32(Dumper($p).'h'); |
111
|
0
|
|
|
|
|
|
my $q = $self->{mem}->get($key); |
112
|
0
|
0
|
|
|
|
|
if(!$q){ |
113
|
0
|
|
|
|
|
|
$q = $self->select_hashref($p); |
114
|
0
|
|
|
|
|
|
$self->{mem}->set($key,$q,$self->{expiration_time}); |
115
|
|
|
|
|
|
|
} |
116
|
0
|
|
|
|
|
|
return $q; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|