phasync
Resource management / Examples

Resource management

Examples

Every example of this topic, with its output.

LruCache#

Example 1
$cache = new LruCache(maxEntries: 10_000, maxBytes: 64 << 20);
$cache->set('user:42', $json, ttl: 60);
$json = $cache->get('user:42');   // null once expired or evicted

Pool#

Example 1
$db = new phasync\Util\Pool(fn () => new PDO($dsn, $user, $password), 10);

// Heavy instances: give memory back 30 s after a burst, and flush what is dropped
$apps = new phasync\Util\Pool(fn () => bootApplication(), 8, window: 30, dispose: fn ($app) => $app->flush());

$rows = $db->use(fn (PDO $pdo) => $pdo->query('SELECT ...')->fetchAll());

$pdo = $db->borrow();          // or by hand
try {
    // ...
} finally {
    $db->release($pdo);
}