Newer Older
171 lines | 3.628kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojo::Asset::Memory;
2
use Mojo::Base 'Mojo::Asset';
3

            
4
use Mojo::Asset::File;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
5
use Mojo::Util 'spurt';
copy gitweblite soruce code
root authored on 2012-11-23
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...
Yuki Kimoto authored on 2013-01-28
15
  # Upgrade if necessary
16
  $self->{content} .= defined $chunk ? $chunk : '';
copy gitweblite soruce code
root authored on 2012-11-23
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...
Yuki Kimoto authored on 2013-01-28
20
  return $file->add_chunk($self->emit(upgrade => $file)->slurp);
copy gitweblite soruce code
root authored on 2012-11-23
21
}
22

            
23
sub contains {
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
24
  my ($self, $str) = @_;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
25

            
copy gitweblite soruce code
root authored on 2012-11-23
26
  my $start = $self->start_range;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
27
  my $pos = index $self->{content}, $str, $start;
copy gitweblite soruce code
root authored on 2012-11-23
28
  $pos -= $start if $start && $pos >= 0;
29
  my $end = $self->end_range;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
30

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
31
  return $end && ($pos + length $str) >= $end ? -1 : $pos;
copy gitweblite soruce code
root authored on 2012-11-23
32
}
33

            
34
sub get_chunk {
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
35
  my ($self, $offset, $max) = @_;
36
  $max = defined $max ? $max : 131072;
copy gitweblite soruce code
root authored on 2012-11-23
37

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
38
  $offset += $self->start_range;
copy gitweblite soruce code
root authored on 2012-11-23
39
  if (my $end = $self->end_range) {
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
40
    $max = $end + 1 - $offset if ($offset + $max) > $end;
copy gitweblite soruce code
root authored on 2012-11-23
41
  }
42

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
43
  return substr shift->{content}, $offset, $max;
copy gitweblite soruce code
root authored on 2012-11-23
44
}
45

            
46
sub move_to {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
47
  my ($self, $to) = @_;
48
  spurt $self->{content}, $to;
copy gitweblite soruce code
root authored on 2012-11-23
49
  return $self;
50
}
51

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

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

            
56
1;
57

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
58
=encoding utf8
59

            
copy gitweblite soruce code
root authored on 2012-11-23
60
=head1 NAME
61

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
62
Mojo::Asset::Memory - In-memory storage for HTTP content
copy gitweblite soruce code
root authored on 2012-11-23
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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
74
L<Mojo::Asset::Memory> is an in-memory storage backend for HTTP content.
copy gitweblite soruce code
root authored on 2012-11-23
75

            
76
=head1 EVENTS
77

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
78
L<Mojo::Asset::Memory> inherits all events from L<Mojo::Asset> and can emit
79
the following new ones.
copy gitweblite soruce code
root authored on 2012-11-23
80

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
81
=head2 upgrade
copy gitweblite soruce code
root authored on 2012-11-23
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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
100
=head2 auto_upgrade
copy gitweblite soruce code
root authored on 2012-11-23
101

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
102
  my $bool = $mem->auto_upgrade;
103
  $mem     = $mem->auto_upgrade($bool);
copy gitweblite soruce code
root authored on 2012-11-23
104

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
105
Try to detect if content size exceeds L</"max_memory_size"> limit and
copy gitweblite soruce code
root authored on 2012-11-23
106
automatically upgrade to a L<Mojo::Asset::File> object.
107

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
108
=head2 max_memory_size
copy gitweblite soruce code
root authored on 2012-11-23
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
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
115
MOJO_MAX_MEMORY_SIZE environment variable or C<262144>.
copy gitweblite soruce code
root authored on 2012-11-23
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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
122
=head2 new
copy gitweblite soruce code
root authored on 2012-11-23
123

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

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
128
=head2 add_chunk
copy gitweblite soruce code
root authored on 2012-11-23
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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
135
=head2 contains
copy gitweblite soruce code
root authored on 2012-11-23
136

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

            
139
Check if asset contains a specific string.
140

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
141
=head2 get_chunk
copy gitweblite soruce code
root authored on 2012-11-23
142

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
143
  my $bytes = $mem->get_chunk($offset);
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
144
  my $bytes = $mem->get_chunk($offset, $max);
copy gitweblite soruce code
root authored on 2012-11-23
145

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
146
Get chunk of data starting from a specific position, defaults to a maximum
147
chunk size of C<131072> bytes.
copy gitweblite soruce code
root authored on 2012-11-23
148

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
149
=head2 move_to
copy gitweblite soruce code
root authored on 2012-11-23
150

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
151
  $mem = $mem->move_to('/home/sri/foo.txt');
copy gitweblite soruce code
root authored on 2012-11-23
152

            
153
Move asset data into a specific file.
154

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
155
=head2 size
copy gitweblite soruce code
root authored on 2012-11-23
156

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

            
159
Size of asset data in bytes.
160

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
161
=head2 slurp
copy gitweblite soruce code
root authored on 2012-11-23
162

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
163
  my $bytes = mem->slurp;
copy gitweblite soruce code
root authored on 2012-11-23
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