phasync
Cache / set

Method

Cache::set

Store $value under $key for every worker, for $ttl seconds or until evicted.

Description#

public Cache::set(
    $key,
    mixed $value,
    $ttl = null
): bool

The value is serialized in the calling worker. Once this returns, every worker has been told to forget the key and fetches the new value when it next reads it.

Parameters#

$key
$value mixed
$ttl = null
Seconds, an interval, or null for no expiry; zero or less deletes the key

Return values#

False when the value is larger than the cache holds bool

Errors / Exceptions#

ExceptionWhen
CacheKeyExceptionFor an invalid key
LogicExceptionDirectly in swerve.php, which the worker serves after: a coroutine started there waits instead

Examples#

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

See also#

  • Cache::getThe value stored under $key, or $default when there is none or it has expired.
  • Cache::deleteRemove $key from the cache of every worker.