Skip to content
php-baremetal REGISTRY
Publish a package

php-baremetal/ext-mem

bm-ext MIT

In-RAM key-value store: shared across requests, wiped on reset.

needs no platform HAL

ext-mem · Recipe

Share state across requests

In the web-server runtime every HTTP request starts with empty PHP variables. Seed mem_* once from the init script, read and update it per request, and keep anything that must outlive a reset in store_*.

The web-server runtime is shared-nothing, like PHP-FPM: every HTTP request is a fresh run of your script, so a variable assigned in one request is not there in the next. mem_* lives below PHP and is. This recipe seeds a value once at boot and keeps a request counter in RAM.

Goal

Every request shows a message computed once at boot and a counter of requests since boot.

What you need

A board with a network connection (Wi-Fi from init.php with ext-wifi, or Ethernet) running a web-server project, and php-baremetal/ext-mem:

bash terminal
composer require php-baremetal/ext-mem

The web-server runtime has an init option: a script, relative to project-src, that runs once after the engine starts and before the HTTP server accepts its first connection.

json composer.json
{
  "name": "acme/web-counter",
  "type": "project",
  "repositories": [
    { "type": "composer", "url": "https://repository.php-baremetal.com", "canonical": true }
  ],
  "require": {
    "php-baremetal/conductor": "^1.0",
    "php-baremetal/ext-mem": "^1.0"
  },
  "config": {
    "allow-plugins": { "php-baremetal/conductor": true },
    "platform": { "php": "8.5.11" }
  },
  "extra": {
    "baremetal": {
      "sdk": "esp32@^2.0",
      "board": "waveshare-esp32-s3-touch-lcd-1.85b",
      "runtime": "web-server",
      "storage": "embedded",
      "options": { "init": "init.php" }
    }
  }
}

The code

init.php is a request like any other: its variables vanish when it ends, but what it writes to mem_* stays. Its output goes to the serial console, not to an HTTP response.

php project-src/init.php: runs once at boot
<?php
echo "[init] seeding mem_*\n";
mem_set('boot_msg', 'initialised once at boot');
mem_set('hits', 0);
php project-src/index.php: runs per request
<?php
$hits = (int) mem_get('hits', 0) + 1;
mem_set('hits', $hits);

header('Content-Type: text/plain');
echo mem_get('boot_msg', '(unset)'), "\n";
echo "request #$hits since boot\n";
echo "mem keys: ", implode(', ', mem_keys()), "\n";

What you will see

At boot the console shows the init script running once, before the server comes up. Then each request reads the seeded message and bumps the counter:

bash terminal: two requests
curl http://BOARD_IP/
# initialised once at boot
# request #1 since boot
# mem keys: boot_msg, hits

curl http://BOARD_IP/
# initialised once at boot
# request #2 since boot
# mem keys: boot_msg, hits

Reset the board and the count restarts from 1.

Persistent settings, volatile copies

To serve a setting that must survive a reset, keep it in store_* and copy it into mem_* at boot. Requests then read RAM only, and flash is touched once per boot. This needs php-baremetal/ext-store as well.

php project-src/init.php: load settings once
<?php
mem_set('settings', [
    'name' => store_get('name', 'device'),
    'unit' => store_get('unit', 'C'),
]);

See ext-store.

Related