Dutch PHP Conference 2027

Yac::delete

(PECL yac >= 1.0.0)

Yac::deleteRemove items from cache

Beschreibung

public function Yac::delete(string|array $key, int $delay = 0): bool

Removes one or more items from the cache.

Hinweis:

Deletion is implemented by marking the entry expired rather than clearing its slot: the entry immediately stops being readable, but the slot stays occupied until the same key is stored again or a later write reclaims the slot. As a result, the used-slot count reported by Yac::info() does not decrease after a deletion, and Yac::dump() still lists the deleted entry until its slot is recycled; a non-zero ttl in the past marks a deleted or expired entry, so when inspecting the output of Yac::dump(), such entries have to be filtered out by the caller.

Parameter-Liste

key

A string key, or an array of keys to be removed.

delay

Number of seconds before the item becomes invalid. When omitted or 0, the item is invalidated immediately. A positive value keeps the item readable for that many seconds before it expires.

Rückgabewerte

Returns true on success, or false if the key was not present in the cache. Because a deletion only marks the entry expired, a key that was deleted but not yet overwritten still counts as present: deleting the same key again returns true.

When an array of keys is given, true is returned only if every key was present; if any key is missing, false is returned.

Beispiele

Beispiel #1 Yac::delete() example

<?php
$yac = new Yac();
$yac->set("foo", "bar");

var_dump($yac->delete("foo"));      // bool(true): marked expired
var_dump($yac->get("foo"));         // bool(false): a miss from now on
var_dump($yac->delete("foo"));      // bool(true) again: the slot has not
                                     // been overwritten yet
var_dump($yac->delete("never"));    // bool(false): was never stored

// a deletion does not free the slot: slots_used does not drop, and
// the expired entry still shows up in the dump
var_dump($yac->info()["slots_used"]); // int(1)
print_r($yac->dump());                // "foo" is still listed; its ttl
                                       // is in the past
?>

Beispiel #2 Delayed deletion

<?php
$yac = new Yac();

// keep the entry readable for 60 more seconds before it expires
$yac->set("tmp", "value");
var_dump($yac->delete("tmp", 60));    // bool(true)
?>

Beispiel #3 Deleting multiple keys at once

<?php
$yac = new Yac();
$yac->set("tmp", "value");

// returns true only when every key was present
var_dump($yac->delete(array("tmp", "nope"))); // bool(false): "nope" missing
?>

Siehe auch

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top