Search by

webservco / memory

ariademur

A PHP component/library. Shares short strings between PHP processes on the same machine, using System V shared memory.

v2.3.0 2026-10-11 16:34 UTC

This package is auto-updated.

Last update: 2026-10-11 16:45:39 UTC


README

A PHP component/library. Please see webservco/sandbox for an example project using these types of components.

Shares short strings between PHP processes on the same machine, using System V shared memory.

  • Requires: ext-shmop

Installation

composer require webservco/memory

Usage

use WebServCo\Memory\Service\SharedMemory\DefaultSharedMemoryService;

// Segment size in bytes, 256 by default.
// \WebServCo\Memory\Contract\SharedMemory\SharedMemoryInterface
$sharedMemory = new DefaultSharedMemoryService(256);

// Process A.
$sharedMemory->write('rate-limit-reached', 'app-status');

// Process B, with its own service instance.
$status = $sharedMemory->read('app-status');

$sharedMemory->delete('app-status');

Contracts

Interface Implementation Purpose
Contract\SharedMemory\SharedMemoryInterface Service\SharedMemory\DefaultSharedMemoryService write(), read() and delete() by identifier.

Namespaces are relative to WebServCo\Memory\.

Notes

The identifier becomes the shared memory key through crc32(). Any process that uses the same identifier and size opens the same segment.

Data is padded with spaces to the segment size. read() trims the padding off, and any leading or trailing whitespace of your own goes with it. Writing more than the segment size fails. Size the segment for the largest value you'll store.

delete() only removes segments this instance has already opened, and returns false for others. Segments last until they are deleted or the machine restarts.

Development

# Start project.
ddev start
# Install dependencies.
ddev c u
# Run all validations.
ddev c c
# Run unit tests.
ddev c test:dox

License

MIT