| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Netstack::Utils::Cache; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
4
|
|
|
|
|
|
|
# 加载扩展模块功能 |
|
5
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
6
|
1
|
|
|
1
|
|
227835
|
use 5.016; |
|
|
1
|
|
|
|
|
3
|
|
|
7
|
1
|
|
|
1
|
|
475
|
use Moose; |
|
|
1
|
|
|
|
|
390704
|
|
|
|
1
|
|
|
|
|
7
|
|
|
8
|
1
|
|
|
1
|
|
6728
|
use namespace::autoclean; |
|
|
1
|
|
|
|
|
6898
|
|
|
|
1
|
|
|
|
|
3
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
11
|
|
|
|
|
|
|
# 定义 Netstack::Utils::Cache 方法属性 |
|
12
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
13
|
|
|
|
|
|
|
has cache => ( |
|
14
|
|
|
|
|
|
|
is => 'ro', |
|
15
|
|
|
|
|
|
|
isa => 'HashRef[Ref]', |
|
16
|
|
|
|
|
|
|
default => sub { {} }, |
|
17
|
|
|
|
|
|
|
); |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
20
|
|
|
|
|
|
|
# get 获取缓存数据 | 查询特定的缓存对象 |
|
21
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
22
|
|
|
|
|
|
|
sub get { |
|
23
|
3
|
|
|
3
|
0
|
14
|
my $self = shift; |
|
24
|
3
|
|
|
|
|
7
|
return $self->locate(@_); |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
28
|
|
|
|
|
|
|
# set 设置缓存数据 | 设置特定的缓存对象 |
|
29
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
30
|
|
|
|
|
|
|
sub set { |
|
31
|
1
|
|
|
1
|
0
|
7
|
my $self = shift; |
|
32
|
1
|
50
|
|
|
|
4
|
confess __PACKAGE__ . " ERROR: 必须提供键值对格式(cacheType, key, value)" if @_ < 2; |
|
33
|
|
|
|
|
|
|
# 获取 |
|
34
|
1
|
|
|
|
|
2
|
my $value = pop; |
|
35
|
1
|
|
|
|
|
2
|
my $lastKey = pop; |
|
36
|
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
3
|
my @keys = @_; |
|
38
|
1
|
|
|
|
|
37
|
my $ref = $self->cache; |
|
39
|
|
|
|
|
|
|
# 提供2个以上入参 |
|
40
|
1
|
|
|
|
|
2
|
my @step; |
|
41
|
1
|
|
|
|
|
3
|
while ( my $key = shift @keys ) { |
|
42
|
2
|
|
|
|
|
5
|
push @step, $key; |
|
43
|
|
|
|
|
|
|
# 检查是否已定义缓存对象 |
|
44
|
2
|
50
|
|
|
|
5
|
if ( not exists $ref->{$key} ) { |
|
45
|
0
|
|
|
|
|
0
|
$ref->{$key} = undef; |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
# 此处实现嵌套 |
|
48
|
2
|
|
|
|
|
2
|
$ref = $ref->{$key}; |
|
49
|
|
|
|
|
|
|
# 格式校验 |
|
50
|
2
|
50
|
33
|
|
|
11
|
if ( defined $ref and ref($ref) ne 'HASH' ) { |
|
51
|
0
|
|
|
|
|
0
|
confess "ERROR: cache->" . join( '->', @step ) . " not a valid HashRef"; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
} |
|
54
|
1
|
|
|
|
|
4
|
$ref->{$lastKey} = $value; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
58
|
|
|
|
|
|
|
# clear 清楚缓存数据 |
|
59
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
60
|
|
|
|
|
|
|
sub clear { |
|
61
|
1
|
|
|
1
|
0
|
6
|
my $self = shift; |
|
62
|
1
|
|
|
|
|
4
|
my @keys = @_; |
|
63
|
|
|
|
|
|
|
# 携带参数则删除具体的缓存对象,否则情况所有的缓存 |
|
64
|
1
|
50
|
|
|
|
3
|
if (@keys) { |
|
65
|
1
|
|
|
|
|
2
|
my $lastKey = pop @keys; |
|
66
|
|
|
|
|
|
|
# 检索缓存对象 |
|
67
|
1
|
|
|
|
|
3
|
my $ref = $self->locate(@keys); |
|
68
|
1
|
50
|
33
|
|
|
7
|
if ( defined $ref and ref($ref) eq 'HASH' ) { |
|
69
|
1
|
|
|
|
|
4
|
delete( $ref->{$lastKey} ); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
else { |
|
73
|
0
|
|
|
|
|
0
|
$self->{cache} = {}; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
78
|
|
|
|
|
|
|
# locate 加载缓存,,只支持单值索引 | 加载特定的缓存对象 |
|
79
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
80
|
|
|
|
|
|
|
sub locate { |
|
81
|
5
|
|
|
5
|
0
|
14
|
my $self = shift; |
|
82
|
|
|
|
|
|
|
# 初始化变量 |
|
83
|
5
|
|
|
|
|
11
|
my @keys = @_; |
|
84
|
5
|
|
|
|
|
109
|
my $ref = $self->cache; |
|
85
|
|
|
|
|
|
|
# 遍历待查询的 keyStr |
|
86
|
5
|
|
|
|
|
21
|
while ( my $key = shift @keys ) { |
|
87
|
15
|
50
|
|
|
|
38
|
if ( not exists $ref->{$key} ) { |
|
88
|
0
|
|
|
|
|
0
|
return; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
# 此处实现嵌套 |
|
91
|
15
|
|
|
|
|
30
|
$ref = $ref->{$key}; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
5
|
|
|
|
|
17
|
return $ref; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
97
|
|
|
|
|
|
|
1; |