copy gitweblite soruce code
|
1 |
package Mojo::Asset::Memory; |
2 |
use Mojo::Base 'Mojo::Asset'; |
|
3 | ||
4 |
use Mojo::Asset::File; |
|
upgraded Mojolicious to v3.7...
|
5 |
use Mojo::Util 'spurt'; |
copy gitweblite soruce code
|
6 | |
7 |
has 'auto_upgrade'; |
|
8 |
has max_memory_size => sub { $ENV{MOJO_MAX_MEMORY_SIZE} || 262144 }; |
|
9 | ||
10 |
sub new { shift->SUPER::new(@_, content => '') } |
|
11 | ||
12 |
sub add_chunk { |
|
13 |
my ($self, $chunk) = @_; |
|
14 | ||
upgraded Mojolicious to v3.7...
|
15 |
# Upgrade if necessary |
16 |
$self->{content} .= defined $chunk ? $chunk : ''; |
|
copy gitweblite soruce code
|
17 |
return $self |
18 |
if !$self->auto_upgrade || $self->size <= $self->max_memory_size; |
|
19 |
my $file = Mojo::Asset::File->new; |
|
upgraded Mojolicious to v3.7...
|
20 |
return $file->add_chunk($self->emit(upgrade => $file)->slurp); |
copy gitweblite soruce code
|
21 |
} |
22 | ||
23 |
sub contains { |
|
upgraded Mojolicious to v3.7...
|
24 |
my ($self, $string) = @_; |
25 | ||
copy gitweblite soruce code
|
26 |
my $start = $self->start_range; |
upgraded Mojolicious to v3.7...
|
27 |
my $pos = index $self->{content}, $string, $start; |
copy gitweblite soruce code
|
28 |
$pos -= $start if $start && $pos >= 0; |
29 |
my $end = $self->end_range; |
|
upgraded Mojolicious to v3.7...
|
30 | |
31 |
return $end && ($pos + length $string) >= $end ? -1 : $pos; |
|
copy gitweblite soruce code
|
32 |
} |
33 | ||
34 |
sub get_chunk { |
|
35 |
my ($self, $start) = @_; |
|
36 | ||
37 |
$start += $self->start_range; |
|
upgraded Mojolicious to v3.7...
|
38 |
my $size = 131072; |
copy gitweblite soruce code
|
39 |
if (my $end = $self->end_range) { |
40 |
$size = $end + 1 - $start if ($start + $size) > $end; |
|
41 |
} |
|
42 | ||
43 |
return substr shift->{content}, $start, $size; |
|
44 |
} |
|
45 | ||
46 |
sub move_to { |
|
upgraded Mojolicious to v3.7...
|
47 |
my ($self, $to) = @_; |
48 |
spurt $self->{content}, $to; |
|
copy gitweblite soruce code
|
49 |
return $self; |
50 |
} |
|
51 | ||
52 |
sub size { length shift->{content} } |
|
53 | ||
54 |
sub slurp { shift->{content} } |
|
55 | ||
56 |
1; |
|
57 | ||
58 |
=head1 NAME |
|
59 | ||
upgraded Mojolicious to v3.7...
|
60 |
Mojo::Asset::Memory - In-memory storage for HTTP content |
copy gitweblite soruce code
|
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 | ||
upgraded Mojolicious to v3.7...
|
72 |
L<Mojo::Asset::Memory> is an in-memory storage backend for HTTP content. |
copy gitweblite soruce code
|
73 | |
74 |
=head1 EVENTS |
|
75 | ||
update Mojolicious and added...
|
76 |
L<Mojo::Asset::Memory> inherits all events from L<Mojo::Asset> and can emit |
77 |
the following new ones. |
|
copy gitweblite soruce code
|
78 | |
update Mojolicious and added...
|
79 |
=head2 upgrade |
copy gitweblite soruce code
|
80 | |
81 |
$mem->on(upgrade => sub { |
|
82 |
my ($mem, $file) = @_; |
|
83 |
... |
|
84 |
}); |
|
85 | ||
86 |
Emitted when asset gets upgraded to a L<Mojo::Asset::File> object. |
|
87 | ||
88 |
$mem->on(upgrade => sub { |
|
89 |
my ($mem, $file) = @_; |
|
90 |
$file->tmpdir('/tmp'); |
|
91 |
}); |
|
92 | ||
93 |
=head1 ATTRIBUTES |
|
94 | ||
95 |
L<Mojo::Asset::Memory> inherits all attributes from L<Mojo::Asset> and |
|
96 |
implements the following new ones. |
|
97 | ||
update Mojolicious and added...
|
98 |
=head2 auto_upgrade |
copy gitweblite soruce code
|
99 | |
100 |
my $upgrade = $mem->auto_upgrade; |
|
101 |
$mem = $mem->auto_upgrade(1); |
|
102 | ||
103 |
Try to detect if content size exceeds C<max_memory_size> limit and |
|
104 |
automatically upgrade to a L<Mojo::Asset::File> object. |
|
105 | ||
update Mojolicious and added...
|
106 |
=head2 max_memory_size |
copy gitweblite soruce code
|
107 | |
108 |
my $size = $mem->max_memory_size; |
|
109 |
$mem = $mem->max_memory_size(1024); |
|
110 | ||
111 |
Maximum size in bytes of data to keep in memory before automatically upgrading |
|
112 |
to a L<Mojo::Asset::File> object, defaults to the value of the |
|
113 |
C<MOJO_MAX_MEMORY_SIZE> environment variable or C<262144>. |
|
114 | ||
115 |
=head1 METHODS |
|
116 | ||
117 |
L<Mojo::Asset::Memory> inherits all methods from L<Mojo::Asset> and implements |
|
118 |
the following new ones. |
|
119 | ||
update Mojolicious and added...
|
120 |
=head2 new |
copy gitweblite soruce code
|
121 | |
122 |
my $mem = Mojo::Asset::Memory->new; |
|
123 | ||
124 |
Construct a new L<Mojo::Asset::Memory> object. |
|
125 | ||
update Mojolicious and added...
|
126 |
=head2 add_chunk |
copy gitweblite soruce code
|
127 | |
128 |
$mem = $mem->add_chunk('foo bar baz'); |
|
129 |
my $file = $mem->add_chunk('abc' x 262144); |
|
130 | ||
131 |
Add chunk of data and upgrade to L<Mojo::Asset::File> object if necessary. |
|
132 | ||
update Mojolicious and added...
|
133 |
=head2 contains |
copy gitweblite soruce code
|
134 | |
135 |
my $position = $mem->contains('bar'); |
|
136 | ||
137 |
Check if asset contains a specific string. |
|
138 | ||
update Mojolicious and added...
|
139 |
=head2 get_chunk |
copy gitweblite soruce code
|
140 | |
update Mojolicious and added...
|
141 |
my $bytes = $mem->get_chunk($offset); |
copy gitweblite soruce code
|
142 | |
143 |
Get chunk of data starting from a specific position. |
|
144 | ||
update Mojolicious and added...
|
145 |
=head2 move_to |
copy gitweblite soruce code
|
146 | |
upgraded Mojolicious to v3.7...
|
147 |
$mem = $mem->move_to('/home/sri/foo.txt'); |
copy gitweblite soruce code
|
148 | |
149 |
Move asset data into a specific file. |
|
150 | ||
update Mojolicious and added...
|
151 |
=head2 size |
copy gitweblite soruce code
|
152 | |
153 |
my $size = $mem->size; |
|
154 | ||
155 |
Size of asset data in bytes. |
|
156 | ||
update Mojolicious and added...
|
157 |
=head2 slurp |
copy gitweblite soruce code
|
158 | |
update Mojolicious and added...
|
159 |
my $bytes = mem->slurp; |
copy gitweblite soruce code
|
160 | |
161 |
Read all asset data at once. |
|
162 | ||
163 |
=head1 SEE ALSO |
|
164 | ||
165 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
166 | ||
167 |
=cut |