phasync
Resource management / Cache

Resource management

Cache

Swerve::cache(): one PSR-16 cache for every worker, read from memory in the worker.

Swerve::cache() returns a Psr\SimpleCache\CacheInterface. Every worker sees the same entries, so a value cached by one request is a hit for the next, whichever worker serves it.

use Swerve\Swerve;

$cache   = Swerve::cache();
$sidebar = $cache->get('home:sidebar');
if (null === $sidebar) {
    $sidebar = render_sidebar();                  // your own code
    $cache->set('home:sidebar', $sidebar, 60);    // 60 seconds
}

set() takes a TTL in seconds or a DateInterval, as PSR-16 says. Entries are stored serialized, so anything serialize() takes can be cached.

How it works#

A worker never keeps a value older than the last write it has been told of.

What it does not do#

See also#

  • Swerve::cacheThe cache every worker shares, held by the master process.
  • Swerve::publishSend a message to every subscriber of $topic, in every worker process of this swerve, this one included.