| 1 | package DBIx::MoCo::Cache; | ||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 13 | my $cache = {}; | |||||||||||||||||||||||||||
| 6 | 13 | my $cache_created = time(); | |||||||||||||||||||||||||||
| 7 | 13 | my $cache_expire = 600; # seconds | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 33 | ▶ | sub new { bless {}, shift } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 80 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 80 | my $self = shift; | |||||||||||||||||||||||||||
| 13 | 80 | my ($k,$v) = @_; | |||||||||||||||||||||||||||
| 14 | 80 | ▶ | $cache->{$k} = $v if defined $k; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 136 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=4 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 136 | my $self = shift; | |||||||||||||||||||||||||||
| 19 | 136 | ▶ | my $k = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 136 | ▶ | if (!$cache_created || ($cache_created + $cache_expire < time())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $cache_created or $cache_created + $cache_expire < time
MC/DC0%0/2
not $cache_created or $cache_created + $cache_expire < time
not $cache_created
$cache_created + $cache_expire < time
Truth table33%1/3
not(A) or B
A= not $cache_createdB= $cache_created + $cache_expire < time
| |||||||||||||||||||||||||||||
| 21 | 0 | $self->clear; | |||||||||||||||||||||||||||
| 22 | #warn 'clear cache'; | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 136 | return $cache->{$k}; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 0 | $cache = {}; | |||||||||||||||||||||||||||
| 29 | 0 | $cache_created = time(); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 43 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 43 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 43 | ▶ | my $k = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | #warn "remove cache $k"; | ||||||||||||||||||||||||||||
| 36 | 43 | $cache->{$k} = undef; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub cache_expire { | ||||||||||||||||||||||||||
|
Subroutine
cache_expire: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | $cache_expire = $_[0] if $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | $cache_expire; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 13 | 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<DBIx::MoCo>, L<Cache> | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Junya Kondo, E<lt>jkondo@hatena.comE<gt> | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||