phasync
Resource management / Examples

Resource management

Examples

Every example of this topic, with its output.

Cache#

Example 1Set, get and expire values
use Swerve\Swerve;

phasync::run(function () {
    $cache = Swerve::cache();                 // PSR-16: Psr\SimpleCache\CacheInterface

    $cache->set('greeting', 'hello', 60);     // 60 seconds
    $cache->set('config', ['debug' => false, 'level' => 3]);   // until evicted

    echo $cache->get('greeting'), "\n";
    var_dump($cache->get('config'));
    var_dump($cache->get('missing', 'a default'));

    $cache->set('greeting', 'gone', 0);       // a ttl of zero or less deletes
    var_dump($cache->has('greeting'));
});
Output
hello
array(2) {
  ["debug"]=>
  bool(false)
  ["level"]=>
  int(3)
}
string(9) "a default"
bool(false)

Cache#

Example 1
$cache = Swerve::cache();
$user  = $cache->get("user.$id") ?? load_user($id);
$cache->set("user.$id", $user, 60);

Cache::get#

Example 1
$user = Swerve::cache()->get("user.$id") ?? load_user($id);

Cache::getMultiple#

Example 1
['a' => $a, 'b' => $b] = Swerve::cache()->getMultiple(['a', 'b']);

Cache::has#

Example 1A stored null and a missing key
use Swerve\Swerve;

phasync::run(function () {
    $cache = Swerve::cache();
    $cache->set('nothing', null);

    var_dump($cache->has('nothing'));         // stored, with the value null
    var_dump($cache->has('never-set'));
    var_dump($cache->get('nothing', 'default'));
});
Output
bool(true)
bool(false)
NULL

Cache::set#

Example 1
Swerve::cache()->set("user.$id", $user, 60);

Cache::setMultiple#

Example 1Several values with one ttl
use Swerve\Swerve;

phasync::run(function () {
    $cache = Swerve::cache();

    $cache->setMultiple(['a' => 1, 'b' => 2, 'c' => 3], new DateInterval('PT5M'));   // one ttl for all

    var_export($cache->getMultiple(['a', 'b', 'x'], 'none'));
    echo "\n";
});
Output
array (
  'a' => 1,
  'b' => 2,
  'x' => 'none',
)

Cache::delete#

Example 1Delete one key, or several
use Swerve\Swerve;

phasync::run(function () {
    $cache = Swerve::cache();
    $cache->setMultiple(['session.1' => 'ann', 'session.2' => 'bob', 'session.3' => 'cy']);

    $cache->delete('session.1');                          // in every worker
    $cache->deleteMultiple(['session.2', 'not-there']);    // true, also for a key not there

    var_export($cache->getMultiple(['session.1', 'session.2', 'session.3']));
    echo "\n";
});
Output
array (
  'session.1' => NULL,
  'session.2' => NULL,
  'session.3' => 'cy',
)

Cache::clear#

Example 1Empty the cache
use Swerve\Swerve;

phasync::run(function () {
    $cache = Swerve::cache();
    $cache->setMultiple(['a' => 1, 'b' => 2]);

    $cache->clear();                          // everything, in every worker

    var_export($cache->getMultiple(['a', 'b']));
    echo "\n";
});
Output
array (
  'a' => NULL,
  'b' => NULL,
)

CacheKeyException#

Example 1Keys the cache refuses
use Swerve\CacheKeyException;
use Swerve\Swerve;

phasync::run(function () {
    foreach (['user.1', 'user:1', '', 'a/b'] as $key) {
        try {
            Swerve::cache()->get($key);
            echo "'$key' is a key\n";
        } catch (CacheKeyException $e) {
            echo "'$key' is not: ", $e->getMessage(), "\n";
        }
    }
});
Output
'user.1' is a key
'user:1' is not: A cache key is a non-empty string without {}()/\@:, not 'user:1'
'' is not: A cache key is a non-empty string without {}()/\@:, not ''
'a/b' is not: A cache key is a non-empty string without {}()/\@:, not 'a/b'

CacheKeyException#

Example 1
try {
    Swerve::cache()->get('user:1');          // ':' is not allowed in a key
} catch (CacheKeyException $e) {
    Swerve::cache()->get('user.1');
}

Swerve::cache#

Example 1Load once, then read from the cache
use Swerve\Swerve;

function load_user(int $id): array
{
    echo "loading user $id from the database\n";

    return ['id' => $id, 'name' => 'Ann'];
}

phasync::run(function () {
    foreach ([1, 1, 1] as $id) {
        $user = Swerve::cache()->get("user.$id");
        if (null === $user) {
            $user = load_user($id);
            Swerve::cache()->set("user.$id", $user, 60);   // for every worker, 60 seconds
        }
        echo "hello, {$user['name']}\n";
    }
});
Output
loading user 1 from the database
hello, Ann
hello, Ann
hello, Ann

Swerve::cache#

Example 1
$cache = Swerve::cache();
$user  = $cache->get("user.$id") ?? load_user($id);
$cache->set("user.$id", $user, 60);