Pyjo.Asset.Memory - In-memory storage for HTTP content

from Pyjo.Asset.Memory import Memory

mem = Memory()
mem.add_chunk(b'foo bar baz')
print(mem.slurp())

# Or import the module, as in Google Python Style Guide

import Pyjo.Asset.Memory

mem = Pyjo.Asset.Memory.new()
mem.add_chunk(b'foo bar baz')
print(mem.slurp())

Pyjo.Asset.Memory is an in-memory storage backend for HTTP content.

Events

Pyjo.Asset.Memory inherits all events from Pyjo.Asset and can emit the following new ones.

upgrade

mem.on('upgrade', lambda mem, file: ...)

Emitted when asset gets upgraded to a Pyjo.Asset.File object.

mem.on('upgrade', lambda mem, file: file.set(tmpdir='/tmp'))

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Asset::Memory of Mojolicious 9.51.

class Pyjo.Asset.Memory.Pyjo_Asset_Memory(*, auto_upgrade: bool | None = None, end_range: int | None = None, max_memory_size: int | None = None, mtime: float | None = None, start_range: int = 0)[source]

Pyjo.Asset.Memory inherits all methods from Pyjo.Asset and implements the following new ones.

add_chunk(chunk: bytes) → Pyjo_Asset[source]
mem = mem.add_chunk(b'foo bar baz')
file = mem.add_chunk(b'abc' * 262144)

Add chunk of data and upgrade to Pyjo.Asset.File object if necessary.

auto_upgrade
boolean = mem.auto_upgrade
mem.auto_upgrade = boolean

Try to detect if content size exceeds max_memory_size limit and automatically upgrade to a Pyjo.Asset.File object.

contains(string: bytes) → int[source]
position = mem.contains(b'bar')

Check if asset contains a specific string.

get_chunk(offset: int, max: int | None = None) → bytes[source]
chunk = mem.get_chunk(offset)
chunk = mem.get_chunk(offset, max)

Get chunk of data starting from a specific position, defaults to a maximum chunk size of 131072 bytes (128KiB).

property max_memory_size: int[source]
size = mem.max_memory_size
mem.max_memory_size = 1024

Maximum size in bytes of data to keep in memory before automatically upgrading to a Pyjo.Asset.File object, defaults to the value of the PYJO_MAX_MEMORY_SIZE environment variable or 262144 (256KiB).

move_to(to: Anything) → Self[source]
mem = mem.move_to('/home/sri/foo.txt')

Move asset data into a specific file.

property mtime: float[source]
mtime = mem.mtime
mem.mtime = 1408567500

Modification time of asset, defaults to the time the program started, as Perl’s $^T.

property size: int[source]
size = mem.size

Size of asset data in bytes.

slurp() → bytes[source]
content = mem.slurp()

Read all asset data at once.

to_file() → Pyjo_Asset_File[source]
file = mem.to_file()

Convert asset to Pyjo.Asset.File object.