line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Asset::Memory; |
2
|
62
|
|
|
62
|
|
67959
|
use Mojo::Base 'Mojo::Asset'; |
|
62
|
|
|
|
|
187
|
|
|
62
|
|
|
|
|
509
|
|
3
|
|
|
|
|
|
|
|
4
|
62
|
|
|
62
|
|
488
|
use Carp 'croak'; |
|
62
|
|
|
|
|
152
|
|
|
62
|
|
|
|
|
2731
|
|
5
|
62
|
|
|
62
|
|
26124
|
use Mojo::Asset::File; |
|
62
|
|
|
|
|
215
|
|
|
62
|
|
|
|
|
631
|
|
6
|
62
|
|
|
62
|
|
490
|
use Mojo::File qw(path); |
|
62
|
|
|
|
|
176
|
|
|
62
|
|
|
|
|
42257
|
|
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
|
2119
|
|
|
2119
|
1
|
5809
|
my ($self, $chunk) = @_; |
14
|
|
|
|
|
|
|
|
15
|
2119
|
100
|
|
|
|
5865
|
croak 'Asset has been upgraded and is now frozen' if $self->{frozen}; |
16
|
2118
|
100
|
100
|
|
|
5608
|
if ($self->auto_upgrade && ($self->size + length $chunk) > $self->max_memory_size) { |
17
|
12
|
|
|
|
|
295
|
$self->emit(upgrade => my $file = Mojo::Asset::File->new)->{frozen} = 1; |
18
|
12
|
|
|
|
|
56
|
return $file->add_chunk($self->slurp . $chunk); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
2106
|
|
|
|
|
8773
|
$self->{content} .= $chunk; |
22
|
2106
|
|
|
|
|
6529
|
return $self; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub contains { |
26
|
85
|
|
|
85
|
1
|
243
|
my ($self, $str) = @_; |
27
|
|
|
|
|
|
|
|
28
|
85
|
|
|
|
|
232
|
my $start = $self->start_range; |
29
|
85
|
|
100
|
|
|
365
|
my $pos = index $self->{content} // '', $str, $start; |
30
|
85
|
100
|
100
|
|
|
256
|
$pos -= $start if $start && $pos >= 0; |
31
|
85
|
|
|
|
|
4187
|
my $end = $self->end_range; |
32
|
|
|
|
|
|
|
|
33
|
85
|
100
|
100
|
|
|
700
|
return $end && ($pos + length $str) >= $end ? -1 : $pos; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get_chunk { |
37
|
2566
|
|
|
2566
|
1
|
5555
|
my ($self, $offset, $max) = @_; |
38
|
2566
|
|
100
|
|
|
10736
|
$max //= 131072; |
39
|
|
|
|
|
|
|
|
40
|
2566
|
|
|
|
|
6555
|
$offset += $self->start_range; |
41
|
2566
|
100
|
|
|
|
6846
|
if (my $end = $self->end_range) { $max = $end + 1 - $offset if ($offset + $max) > $end } |
|
20
|
100
|
|
|
|
63
|
|
42
|
|
|
|
|
|
|
|
43
|
2566
|
|
100
|
|
|
12626
|
return substr shift->{content} // '', $offset, $max; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
6
|
50
|
100
|
6
|
1
|
933
|
sub move_to { path($_[1])->spurt($_[0]{content} // '') and return $_[0] } |
47
|
|
|
|
|
|
|
|
48
|
3066
|
|
100
|
3066
|
1
|
16326
|
sub size { length(shift->{content} // '') } |
49
|
|
|
|
|
|
|
|
50
|
1147
|
|
100
|
1147
|
1
|
9964
|
sub slurp { shift->{content} // '' } |
51
|
|
|
|
|
|
|
|
52
|
6
|
|
|
6
|
1
|
174
|
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 |