webservco / memory
A PHP component/library. Shares short strings between PHP processes on the same machine, using System V shared memory.
Requires
- php: ^8.4
- ext-shmop: *
Requires (Dev)
- pds/skeleton: ^1
- phan/phan: ^6
- php-parallel-lint/php-console-highlighter: ^1
- php-parallel-lint/php-parallel-lint: ^1
- phpmd/phpmd: ^3
- phpstan/phpstan: ^2
- phpstan/phpstan-deprecation-rules: ^2
- phpstan/phpstan-phpunit: ^2
- phpstan/phpstan-strict-rules: ^2
- phpunit/phpunit: ^13
- slevomat/coding-standard: ^8
- squizlabs/php_codesniffer: ^4
- vimeo/psalm: ^6
- webservco/coding-standards: ^2
- webservco/component-common: ^2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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