NAME

Mojo::MemoryMap - Safely use anonymous memory mapped segments

SYNOPSIS

  use Mojo::MemoryMap;

  my $map = Mojo::MemoryMap->new(4096);
  say $map->usage;
  $map->writer->store({foo => 123});
  say $map->writer->fetch->{foo};
  say $map->writer->change(sub { delete $_->{foo} });
  say $map->usage;

DESCRIPTION

Mojo::MemoryMap uses File::Map to allow you to safely cache mutable data structures in anonymous mapped memory segments, and share it between multiple processes.

METHODS

Mojo::MemoryMap inherits all methods from Mojo::Base and implements the following new ones.

new

  my $map = Mojo::MemoryMap->new;
  my $map = Mojo::MemoryMap->new(4096);

Construct a new Mojo::MemoryMap object, defaults to a "size" of 52428800 bytes (50 MiB).

size

  my $size = $map->size;

Size of anonymous memory segment in bytes.

usage

  my $usage = $map->usage;

Current usage of anonymous memory segment in bytes.

writer

  my $writer = $map->writer;

Acquire exclusive lock and return Mojo::MemoryMap::Writer object. Allowing the shared data structure to be retrieved and modified safely. The lock is released when the writer object is destroyed.

  # Retrieve data
  my $data = $map->writer->fetch;

  # Modify data safely
  my $writer = $map->writer;
  my $data = $writer->fetch;
  $data->{foo} += 23;
  $writer->store($data);
  undef $writer;

  # Modify data safely (with less code)
  $map->writer->change(sub { $_->{foo} += 23 });

SEE ALSO

Mojolicious::Plugin::Status, Mojolicious::Guides, https://mojolicious.org.