line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::MoCo::Cache; |
2
|
16
|
|
|
16
|
|
61658
|
use strict; |
|
16
|
|
|
|
|
31
|
|
|
16
|
|
|
|
|
577
|
|
3
|
16
|
|
|
16
|
|
89
|
use warnings; |
|
16
|
|
|
|
|
35
|
|
|
16
|
|
|
|
|
5265
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
my $cache = {}; |
6
|
|
|
|
|
|
|
my $cache_created = time(); |
7
|
|
|
|
|
|
|
my $cache_expire = 600; # seconds |
8
|
|
|
|
|
|
|
|
9
|
34
|
|
|
34
|
0
|
33070
|
sub new { bless {}, shift } |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub set { |
12
|
88
|
|
|
88
|
0
|
5550
|
my $self = shift; |
13
|
88
|
|
|
|
|
176
|
my ($k,$v) = @_; |
14
|
88
|
50
|
|
|
|
610
|
$cache->{$k} = $v if defined $k; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub get { |
18
|
158
|
|
|
158
|
0
|
271
|
my $self = shift; |
19
|
158
|
50
|
|
|
|
407
|
my $k = shift or return; |
20
|
158
|
50
|
33
|
|
|
960
|
if (!$cache_created || ($cache_created + $cache_expire < time())) { |
21
|
0
|
|
|
|
|
0
|
$self->clear; |
22
|
|
|
|
|
|
|
#warn 'clear cache'; |
23
|
|
|
|
|
|
|
} |
24
|
158
|
|
|
|
|
1198
|
return $cache->{$k}; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub clear { |
28
|
0
|
|
|
0
|
0
|
0
|
$cache = {}; |
29
|
0
|
|
|
|
|
0
|
$cache_created = time(); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub remove { |
33
|
44
|
|
|
44
|
0
|
1391
|
my $self = shift; |
34
|
44
|
50
|
|
|
|
185
|
my $k = shift or return; |
35
|
|
|
|
|
|
|
#warn "remove cache $k"; |
36
|
44
|
|
|
|
|
313
|
$cache->{$k} = undef; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub cache_expire { |
40
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
41
|
0
|
0
|
|
|
|
|
$cache_expire = $_[0] if $_[0]; |
42
|
0
|
|
|
|
|
|
$cache_expire; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
1; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 NAME |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
DBIx::MoCo::Cache - Simple Cache for DBIx::MoCo |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 SYNOPSIS |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $c = DBIx::MoCo::Cache->new; |
54
|
|
|
|
|
|
|
my $u = User->new(user_id => '123'); |
55
|
|
|
|
|
|
|
my $oid = $u->object_id; |
56
|
|
|
|
|
|
|
$c->set($oid, $u); |
57
|
|
|
|
|
|
|
my $o = $c->get($oid); # $o is $u |
58
|
|
|
|
|
|
|
$c->remove($oid); # flush |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 SEE ALSO |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
L, L |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 AUTHOR |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Junya Kondo, Ejkondo@hatena.comE |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Copyright (C) Hatena Inc. All Rights Reserved. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
This library is free software; you may redistribute it and/or modify |
73
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |