1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
# -*- cperl -*-
# -----------------------------------------------------------------------------
# Tools::FileCache, Data shared file cache service.
# -----------------------------------------------------------------------------
# $Id: FileCache.pm 11365 2008-05-10 14:58:28Z topia $
# -----------------------------------------------------------------------------
# copyright (C) 2003 Topia <topia@clovery.jp>. all rights reserved.
package Tools::FileCache;
use strict;
use warnings;
use RunLoop;
use Carp;
use Tiarra::SharedMixin;
use Module::Use qw(Tools::FileCache::EachFile);
use Tools::FileCache::EachFile;
our $_shared_instance;
sub _new {
my $class = shift;
my ($this) = {
files => {},
timer => undef,
};
bless $this, $class;
return $this;
}
sub find_file {
my ($class_or_this, $fpath, $mode, $charset) = @_;
my $this = $class_or_this->_this;
my $file = $this->{files}->{$fpath};
if (defined($file)) {
# とりあえずファイルは存在した。
my $obj = $file->{$mode};
if (defined($obj)) {
# そのモードも存在した。オブジェクトを返す。
return $obj;
} else {
# そのモードは存在しなかった。登録して返す。
return $this->_register_inner($fpath, $mode, $charset);
}
} else {
# ファイルは存在しない。登録して返す。
return $this->_register_inner($fpath, $mode, $charset);
}
}
sub register {
my ($class_or_this, $fpath, $mode, $charset) = @_;
my $this = $class_or_this->_this;
my $file = $this->find_file($fpath, $mode, $charset);
if (defined $file) {
# ファイルがあった or ファイルを登録した。
# 参照回数を増やして返す。
$file->register();
return $file;
} else {
# ファイルの登録が出来なかった。
return undef;
}
}
sub unregister {
my ($class_or_this, $fpath) = @_;
my $this = $class_or_this->_this;
my $file = $this->{files}->{$fpath};
if (defined($file)) {
$file->unregister();
return 0;
} else {
croak('file "' . $fpath . '" has not registered yet!');
}
}
sub _register_inner {
my ($class_or_this, $fpath, $mode, $charset) = @_;
my $this = $class_or_this->_this;
my $obj = Tools::FileCache::EachFile->new($this, $fpath, $mode, $charset);
if (defined $obj) {
$this->{files}->{$fpath} = {} unless (defined($this->{files}->{$fpath}));
$this->{files}->{$fpath}->{$mode} = $obj;
$this->_install_timer();
return $obj;
} else {
return undef;
}
}
sub main_loop {
my $this = shift;
# check expire
foreach my $key (keys(%{$this->{files}})) {
my $file = $this->{files}->{$key};
foreach my $mode (keys(%$file)) {
my $obj = $file->{$mode};
if ($obj->can_remove() && ($obj->expire() < time())) {
# expired.
$obj->clean();
delete $this->{files}->{$key}->{$mode};
}
}
if (scalar(keys(%$file)) == 0) {
delete $this->{files}->{$key};
}
}
# check struct-size
if (scalar(keys(%{$this->{files}})) == 0) {
$this->_uninstall_timer();
}
}
sub destruct {
my $this = shared;
# expire all
foreach my $key (keys(%{$this->{files}})) {
my $file = $this->{files}->{$key};
foreach my $mode (keys(%$file)) {
my $obj = $file->{$mode};
$obj->clean();
delete $this->{files}->{$key}->{$mode};
}
delete $this->{files}->{$key};
}
# re-run main_loop (for uninstall timer)
$this->main_loop();
}
# misc/timer
sub _check_timer {
my $this = shift;
return defined($this->{timer});
}
sub _install_timer {
my $this = shift;
unless ($this->_check_timer) {
$this->{timer} = Timer->new(
Interval => 30,
Repeat => 1,
Code => sub {
my $timer = shift;
$this->main_loop();
},
)->install();
}
return 0;
}
sub _uninstall_timer {
my $this = shift;
if ($this->_check_timer()) {
$this->{timer}->uninstall;
$this->{timer} = undef;
}
return 0;
}
1;
|