Resource management
Examples
Every example of this topic, with its output.
Cache#
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#
$cache = Swerve::cache();
$user = $cache->get("user.$id") ?? load_user($id);
$cache->set("user.$id", $user, 60);Cache::get#
$user = Swerve::cache()->get("user.$id") ?? load_user($id);Cache::getMultiple#
['a' => $a, 'b' => $b] = Swerve::cache()->getMultiple(['a', 'b']);Cache::has#
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#
Swerve::cache()->set("user.$id", $user, 60);Cache::setMultiple#
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#
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#
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#
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#
try {
Swerve::cache()->get('user:1'); // ':' is not allowed in a key
} catch (CacheKeyException $e) {
Swerve::cache()->get('user.1');
}Swerve::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#
$cache = Swerve::cache();
$user = $cache->get("user.$id") ?? load_user($id);
$cache->set("user.$id", $user, 60);