Newer Older
167 lines | 3.498kb
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 {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
24
  my ($self, $string) = @_;
25

            
copy gitweblite soruce code
root authored on 2012-11-23
26
  my $start = $self->start_range;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
27
  my $pos = index $self->{content}, $string, $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

            
31
  return $end && ($pos + length $string) >= $end ? -1 : $pos;
copy gitweblite soruce code
root authored on 2012-11-23
32
}
33

            
34
sub get_chunk {
35
  my ($self, $start) = @_;
36

            
37
  $start += $self->start_range;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
38
  my $size = 131072;
copy gitweblite soruce code
root authored on 2012-11-23
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...
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

            
58
=head1 NAME
59

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

            
74
=head1 EVENTS
75

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
79
=head2 upgrade
copy gitweblite soruce code
root authored on 2012-11-23
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...
Yuki Kimoto authored on 2013-03-20
98
=head2 auto_upgrade
copy gitweblite soruce code
root authored on 2012-11-23
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...
Yuki Kimoto authored on 2013-03-20
106
=head2 max_memory_size
copy gitweblite soruce code
root authored on 2012-11-23
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...
Yuki Kimoto authored on 2013-03-20
120
=head2 new
copy gitweblite soruce code
root authored on 2012-11-23
121

            
122
  my $mem = Mojo::Asset::Memory->new;
123

            
124
Construct a new L<Mojo::Asset::Memory> object.
125

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

            
135
  my $position = $mem->contains('bar');
136

            
137
Check if asset contains a specific string.
138

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
141
  my $bytes = $mem->get_chunk($offset);
copy gitweblite soruce code
root authored on 2012-11-23
142

            
143
Get chunk of data starting from a specific position.
144

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

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

            
149
Move asset data into a specific file.
150

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

            
153
  my $size = $mem->size;
154

            
155
Size of asset data in bytes.
156

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

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