line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Netstack::Utils::Cache; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
4
|
|
|
|
|
|
|
# 加载扩展模块功能 |
5
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
6
|
1
|
|
|
1
|
|
228446
|
use 5.016; |
|
1
|
|
|
|
|
3
|
|
7
|
1
|
|
|
1
|
|
426
|
use Moose; |
|
1
|
|
|
|
|
376452
|
|
|
1
|
|
|
|
|
5
|
|
8
|
1
|
|
|
1
|
|
7155
|
use namespace::autoclean; |
|
1
|
|
|
|
|
6755
|
|
|
1
|
|
|
|
|
4
|
|
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
|
15
|
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
|
|
|
|
|
3
|
my $value = pop; |
35
|
1
|
|
|
|
|
1
|
my $lastKey = pop; |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
3
|
my @keys = @_; |
38
|
1
|
|
|
|
|
23
|
my $ref = $self->cache; |
39
|
|
|
|
|
|
|
# 提供2个以上入参 |
40
|
1
|
|
|
|
|
2
|
my @step; |
41
|
1
|
|
|
|
|
4
|
while ( my $key = shift @keys ) { |
42
|
2
|
|
|
|
|
3
|
push @step, $key; |
43
|
|
|
|
|
|
|
# 检查是否已定义缓存对象 |
44
|
2
|
50
|
|
|
|
6
|
if ( not exists $ref->{$key} ) { |
45
|
0
|
|
|
|
|
0
|
$ref->{$key} = undef; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
# 此处实现嵌套 |
48
|
2
|
|
|
|
|
2
|
$ref = $ref->{$key}; |
49
|
|
|
|
|
|
|
# 格式校验 |
50
|
2
|
50
|
33
|
|
|
10
|
if ( defined $ref and ref($ref) ne 'HASH' ) { |
51
|
0
|
|
|
|
|
0
|
confess "ERROR: cache->" . join( '->', @step ) . " not a valid HashRef"; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
1
|
|
|
|
|
3
|
$ref->{$lastKey} = $value; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
58
|
|
|
|
|
|
|
# clear 清楚缓存数据 |
59
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
60
|
|
|
|
|
|
|
sub clear { |
61
|
1
|
|
|
1
|
0
|
8
|
my $self = shift; |
62
|
1
|
|
|
|
|
3
|
my @keys = @_; |
63
|
|
|
|
|
|
|
# 携带参数则删除具体的缓存对象,否则情况所有的缓存 |
64
|
1
|
50
|
|
|
|
3
|
if (@keys) { |
65
|
1
|
|
|
|
|
3
|
my $lastKey = pop @keys; |
66
|
|
|
|
|
|
|
# 检索缓存对象 |
67
|
1
|
|
|
|
|
12
|
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
|
13
|
my $self = shift; |
82
|
|
|
|
|
|
|
# 初始化变量 |
83
|
5
|
|
|
|
|
13
|
my @keys = @_; |
84
|
5
|
|
|
|
|
115
|
my $ref = $self->cache; |
85
|
|
|
|
|
|
|
# 遍历待查询的 keyStr |
86
|
5
|
|
|
|
|
14
|
while ( my $key = shift @keys ) { |
87
|
15
|
50
|
|
|
|
24
|
if ( not exists $ref->{$key} ) { |
88
|
0
|
|
|
|
|
0
|
return; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
# 此处实现嵌套 |
91
|
15
|
|
|
|
|
26
|
$ref = $ref->{$key}; |
92
|
|
|
|
|
|
|
} |
93
|
5
|
|
|
|
|
17
|
return $ref; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
97
|
|
|
|
|
|
|
1; |