Newer Older
171 lines | 3.592kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojo::Asset::Memory;
2
use Mojo::Base 'Mojo::Asset';
3

            
4
use Mojo::Asset::File;
5
use Mojo::Util 'spurt';
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

            
15
  # Upgrade if necessary
16
  $self->{content} .= $chunk // '';
17
  return $self
18
    if !$self->auto_upgrade || $self->size <= $self->max_memory_size;
19
  my $file = Mojo::Asset::File->new;
20
  return $file->add_chunk($self->emit(upgrade => $file)->slurp);
21
}
22

            
23
sub contains {
24
  my ($self, $str) = @_;
25

            
26
  my $start = $self->start_range;
27
  my $pos = index $self->{content}, $str, $start;
28
  $pos -= $start if $start && $pos >= 0;
29
  my $end = $self->end_range;
30

            
31
  return $end && ($pos + length $str) >= $end ? -1 : $pos;
32
}
33

            
34
sub get_chunk {
35
  my ($self, $offset, $max) = @_;
36
  $max //= 131072;
37

            
38
  $offset += $self->start_range;
39
  if (my $end = $self->end_range) {
40
    $max = $end + 1 - $offset if ($offset + $max) > $end;
41
  }
42

            
43
  return substr shift->{content}, $offset, $max;
44
}
45

            
46
sub move_to {
47
  my ($self, $to) = @_;
48
  spurt $self->{content}, $to;
49
  return $self;
50
}
51

            
52
sub size { length shift->{content} }
53

            
54
sub slurp { shift->{content} }
55

            
56
1;
57

            
58
=encoding utf8
59

            
60
=head1 NAME
61

            
62
Mojo::Asset::Memory - In-memory storage for HTTP content
63

            
64
=head1 SYNOPSIS
65

            
66
  use Mojo::Asset::Memory;
67

            
68
  my $mem = Mojo::Asset::Memory->new;
69
  $mem->add_chunk('foo bar baz');
70
  say $mem->slurp;
71

            
72
=head1 DESCRIPTION
73

            
74
L<Mojo::Asset::Memory> is an in-memory storage backend for HTTP content.
75

            
76
=head1 EVENTS
77

            
78
L<Mojo::Asset::Memory> inherits all events from L<Mojo::Asset> and can emit
79
the following new ones.
80

            
81
=head2 upgrade
82

            
83
  $mem->on(upgrade => sub {
84
    my ($mem, $file) = @_;
85
    ...
86
  });
87

            
88
Emitted when asset gets upgraded to a L<Mojo::Asset::File> object.
89

            
90
  $mem->on(upgrade => sub {
91
    my ($mem, $file) = @_;
92
    $file->tmpdir('/tmp');
93
  });
94

            
95
=head1 ATTRIBUTES
96

            
97
L<Mojo::Asset::Memory> inherits all attributes from L<Mojo::Asset> and
98
implements the following new ones.
99

            
100
=head2 auto_upgrade
101

            
102
  my $bool = $mem->auto_upgrade;
103
  $mem     = $mem->auto_upgrade($bool);
104

            
105
Try to detect if content size exceeds L</"max_memory_size"> limit and
106
automatically upgrade to a L<Mojo::Asset::File> object.
107

            
108
=head2 max_memory_size
109

            
110
  my $size = $mem->max_memory_size;
111
  $mem     = $mem->max_memory_size(1024);
112

            
113
Maximum size in bytes of data to keep in memory before automatically upgrading
114
to a L<Mojo::Asset::File> object, defaults to the value of the
115
MOJO_MAX_MEMORY_SIZE environment variable or C<262144>.
116

            
117
=head1 METHODS
118

            
119
L<Mojo::Asset::Memory> inherits all methods from L<Mojo::Asset> and implements
120
the following new ones.
121

            
122
=head2 new
123

            
124
  my $mem = Mojo::Asset::Memory->new;
125

            
126
Construct a new L<Mojo::Asset::Memory> object.
127

            
128
=head2 add_chunk
129

            
130
  $mem     = $mem->add_chunk('foo bar baz');
131
  my $file = $mem->add_chunk('abc' x 262144);
132

            
133
Add chunk of data and upgrade to L<Mojo::Asset::File> object if necessary.
134

            
135
=head2 contains
136

            
137
  my $position = $mem->contains('bar');
138

            
139
Check if asset contains a specific string.
140

            
141
=head2 get_chunk
142

            
143
  my $bytes = $mem->get_chunk($offset);
144
  my $bytes = $mem->get_chunk($offset, $max);
145

            
146
Get chunk of data starting from a specific position, defaults to a maximum
147
chunk size of C<131072> bytes.
148

            
149
=head2 move_to
150

            
151
  $mem = $mem->move_to('/home/sri/foo.txt');
152

            
153
Move asset data into a specific file.
154

            
155
=head2 size
156

            
157
  my $size = $mem->size;
158

            
159
Size of asset data in bytes.
160

            
161
=head2 slurp
162

            
163
  my $bytes = mem->slurp;
164

            
165
Read all asset data at once.
166

            
167
=head1 SEE ALSO
168

            
169
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
170

            
171
=cut