line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Asset::Memory; |
2
|
63
|
|
|
63
|
|
68924
|
use Mojo::Base 'Mojo::Asset'; |
|
63
|
|
|
|
|
176
|
|
|
63
|
|
|
|
|
606
|
|
3
|
|
|
|
|
|
|
|
4
|
63
|
|
|
63
|
|
492
|
use Carp 'croak'; |
|
63
|
|
|
|
|
179
|
|
|
63
|
|
|
|
|
2780
|
|
5
|
63
|
|
|
63
|
|
27052
|
use Mojo::Asset::File; |
|
63
|
|
|
|
|
218
|
|
|
63
|
|
|
|
|
661
|
|
6
|
63
|
|
|
63
|
|
496
|
use Mojo::File qw(path); |
|
63
|
|
|
|
|
176
|
|
|
63
|
|
|
|
|
44527
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has 'auto_upgrade'; |
9
|
|
|
|
|
|
|
has max_memory_size => sub { $ENV{MOJO_MAX_MEMORY_SIZE} || 262144 }; |
10
|
|
|
|
|
|
|
has mtime => sub {$^T}; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub add_chunk { |
13
|
2143
|
|
|
2143
|
1
|
6008
|
my ($self, $chunk) = @_; |
14
|
|
|
|
|
|
|
|
15
|
2143
|
100
|
|
|
|
5741
|
croak 'Asset has been upgraded and is now frozen' if $self->{frozen}; |
16
|
2142
|
100
|
100
|
|
|
5733
|
if ($self->auto_upgrade && ($self->size + length $chunk) > $self->max_memory_size) { |
17
|
12
|
|
|
|
|
153
|
$self->emit(upgrade => my $file = Mojo::Asset::File->new)->{frozen} = 1; |
18
|
12
|
|
|
|
|
52
|
return $file->add_chunk($self->slurp . $chunk); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
2130
|
|
|
|
|
8693
|
$self->{content} .= $chunk; |
22
|
2130
|
|
|
|
|
6636
|
return $self; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub contains { |
26
|
85
|
|
|
85
|
1
|
216
|
my ($self, $str) = @_; |
27
|
|
|
|
|
|
|
|
28
|
85
|
|
|
|
|
271
|
my $start = $self->start_range; |
29
|
85
|
|
100
|
|
|
376
|
my $pos = index $self->{content} // '', $str, $start; |
30
|
85
|
100
|
100
|
|
|
308
|
$pos -= $start if $start && $pos >= 0; |
31
|
85
|
|
|
|
|
233
|
my $end = $self->end_range; |
32
|
|
|
|
|
|
|
|
33
|
85
|
100
|
100
|
|
|
667
|
return $end && ($pos + length $str) >= $end ? -1 : $pos; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get_chunk { |
37
|
2596
|
|
|
2596
|
1
|
5638
|
my ($self, $offset, $max) = @_; |
38
|
2596
|
|
100
|
|
|
10599
|
$max //= 131072; |
39
|
|
|
|
|
|
|
|
40
|
2596
|
|
|
|
|
6682
|
$offset += $self->start_range; |
41
|
2596
|
100
|
|
|
|
6700
|
if (my $end = $self->end_range) { $max = $end + 1 - $offset if ($offset + $max) > $end } |
|
22
|
100
|
|
|
|
113
|
|
42
|
|
|
|
|
|
|
|
43
|
2596
|
|
100
|
|
|
12491
|
return substr shift->{content} // '', $offset, $max; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
6
|
50
|
100
|
6
|
1
|
838
|
sub move_to { path($_[1])->spew($_[0]{content} // '') and return $_[0] } |
47
|
|
|
|
|
|
|
|
48
|
3102
|
|
100
|
3102
|
1
|
16284
|
sub size { length(shift->{content} // '') } |
49
|
|
|
|
|
|
|
|
50
|
1156
|
|
100
|
1156
|
1
|
10358
|
sub slurp { shift->{content} // '' } |
51
|
|
|
|
|
|
|
|
52
|
6
|
|
|
6
|
1
|
46
|
sub to_file { Mojo::Asset::File->new->add_chunk(shift->slurp) } |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
1; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=encoding utf8 |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 NAME |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Mojo::Asset::Memory - In-memory storage for HTTP content |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 SYNOPSIS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
use Mojo::Asset::Memory; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $mem = Mojo::Asset::Memory->new; |
67
|
|
|
|
|
|
|
$mem->add_chunk('foo bar baz'); |
68
|
|
|
|
|
|
|
say $mem->slurp; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 DESCRIPTION |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
L is an in-memory storage backend for HTTP content. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 EVENTS |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
L inherits all events from L and can emit the following new ones. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 upgrade |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
$mem->on(upgrade => sub ($mem, $file) {...}); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Emitted when asset gets upgraded to a L object. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$mem->on(upgrade => sub ($mem, $file) { $file->tmpdir('/tmp') }); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
L inherits all attributes from L and implements the following new ones. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 auto_upgrade |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $bool = $mem->auto_upgrade; |
93
|
|
|
|
|
|
|
$mem = $mem->auto_upgrade($bool); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Try to detect if content size exceeds L"max_memory_size"> limit and automatically upgrade to a L |
96
|
|
|
|
|
|
|
object. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 max_memory_size |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
my $size = $mem->max_memory_size; |
101
|
|
|
|
|
|
|
$mem = $mem->max_memory_size(1024); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Maximum size in bytes of data to keep in memory before automatically upgrading to a L object, |
104
|
|
|
|
|
|
|
defaults to the value of the C environment variable or C<262144> (256KiB). |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 mtime |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $mtime = $mem->mtime; |
109
|
|
|
|
|
|
|
$mem = $mem->mtime(1408567500); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Modification time of asset, defaults to the value of C<$^T>. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 METHODS |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
L inherits all methods from L and implements the following new ones. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 add_chunk |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
$mem = $mem->add_chunk('foo bar baz'); |
120
|
|
|
|
|
|
|
my $file = $mem->add_chunk('abc' x 262144); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Add chunk of data and upgrade to L object if necessary. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 contains |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $position = $mem->contains('bar'); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Check if asset contains a specific string. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 get_chunk |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
my $bytes = $mem->get_chunk($offset); |
133
|
|
|
|
|
|
|
my $bytes = $mem->get_chunk($offset, $max); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Get chunk of data starting from a specific position, defaults to a maximum chunk size of C<131072> bytes (128KiB). |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 move_to |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
$mem = $mem->move_to('/home/sri/foo.txt'); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Move asset data into a specific file. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 size |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $size = $mem->size; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Size of asset data in bytes. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 slurp |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $bytes = $mem->slurp; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Read all asset data at once. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 to_file |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my $file = $mem->to_file; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Convert asset to L object. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 SEE ALSO |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
L, L, L. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |