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