Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rogerw99's avatar

What is the difference between Cache::add and Cache::put

It looks like Cache:add is identical to Cache::put, except Cache add will not do anything if the item is already cached.

Is this correct?

0 likes
4 replies
bashy's avatar

Yeah that's it.

Docs;

The add method will only add the item to the cache if it does not already exist in the cache store

BulentSezer's avatar

So this means that, if the given 'key' already exists at the cache the Cache::put will create another instance of the 'key' with a new expiration value (if a value provided, else infinitely) . Is it so?

If yes, one shall prefer using the Cache:add to have a single instance of the key if the design relies on such.

1 like

Please or to participate in this conversation.