Core\Cache\Store::forget
Removes the value under a key from a cache store.
After forget the key returns null again, until something writes it. Forgetting a key that has no value is allowed and does nothing.
forget returns nothing, not even whether there was a value. A cached value may be gone at any time anyway, so an answer here would tell your program nothing it can use.
Each store removes only its own values. A key removed from the memory cache of one server process is still in the memory cache of every other server process. So forget on Core\Cache::memory is not a way to make every server see a change at once.
The store has no way to remove several keys at once. Your program removes them one by one, with the keys it knows it wrote.
The examples below show removing one key first, then removing a group of keys, then removing a value after the data behind it changed.
Parameters
$keystringThe name to delete. Ataintedstring is allowed, as onput.
Return value
Returns void. Nothing. If there is no entry under the key, the call does nothing. An entry can be missing at any time, so the call does not tell you whether there was one.
Errors
IOErrorOnly on asharedcache: the store cannot be reached, or it does not accept the command.
Examples
Removing one key
<?nvs// Shows how to remove one value from a cache store.
var $cache = Core\Cache::memory();$cache->put('greeting', 'hello');
// The value is there. This prints hello.var $before = $cache->get('greeting') as string;echo $before, "\n";
// `forget` removes the value under the key. The read after it returns `null`,// so this prints gone.$cache->forget('greeting');var $after = $cache->get('greeting') ?? 'gone';echo $after as string, "\n";
// Forgetting a key that has no value does nothing. This prints still gone.$cache->forget('greeting');$cache->forget('nothing-wrote-this');var $again = $cache->get('greeting') ?? 'still gone';echo $again as string, "\n";
// The key can be used again. This prints hello again.$cache->put('greeting', 'hello again');var $written = $cache->get('greeting') as string;echo $written, "\n";hellogonestill gonehello againRemoving a group of keys
<?nvs// Shows how to remove several values. A store removes one key at a time, so a// group of keys is a loop over the keys your program wrote.
var $cache = Core\Cache::memory();array<string> $ids = ['4711', '4712', '4713'];
foreach ($ids as string $id) { $cache->put('profile-' . $id, 'the profile of user ' . $id);}
// All three values are in the store. This prints 3.uint $stored = 0;foreach ($ids as string $id) { if ($cache->get('profile-' . $id) != null) { $stored = $stored + 1; }}echo $stored, "\n";
// One `forget` for each key. This prints 0.foreach ($ids as string $id) { $cache->forget('profile-' . $id);}uint $left = 0;foreach ($ids as string $id) { if ($cache->get('profile-' . $id) != null) { $left = $left + 1; }}echo $left, "\n";30Removing a value after a change
<?nvs// Shows how a program removes a cached value after the data behind it// changed, so that the next read builds the value again.
class Articles{ // A real program reads the database here. This example is given the title // the database holds, to keep the example short. public static function titleOf(string $id, string $inDatabase): tainted string { var $cache = Core\Cache::memory(); string $key = 'article-' . $id;
var $held = $cache->get($key); if ($held != null) { return $held as string; }
$cache->put($key, $inDatabase, { ttl: 1h }); return $inDatabase; }
// Your program calls this after it saved the article. public static function saved(string $id): void { var $cache = Core\Cache::memory(); $cache->forget('article-' . $id); }}
// The first read stores the title. This prints Bread and salt.echo Articles::titleOf('7', 'Bread and salt'), "\n";
// The title in the database is new, but the store still has the old one. This// prints Bread and salt.echo Articles::titleOf('7', 'Bread, salt and water'), "\n";
// After saving, the program removes the entry. The next read builds it again,// so this prints Bread, salt and water.Articles::saved('7');echo Articles::titleOf('7', 'Bread, salt and water'), "\n";Bread and saltBread and saltBread, salt and water