Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apc_add() versus apc_store()

Tags:

php

apc

I know that apc_store() overwrites the key if it already exists, so I know the difference, my question is just: What's the better method/best practice?

like image 277
Gerben Jacobs Avatar asked Apr 05 '11 09:04

Gerben Jacobs


3 Answers

The only difference between those two functions is that apc_add() will not overwrite an existing entry.
apc_store(), on the other hand, will overwrite an existing entry.

So, which one should your use, between apc_add() and apc_store() ?
Well, it all depends on your needs : do you want an existing entry to be overwritten, or not ?

like image 113
Pascal MARTIN Avatar answered Sep 28 '22 14:09

Pascal MARTIN


In addition to what @Pascal MARTIN writes, there is a major practical difference between them.

In a heavily concurrent environment, e.g. when using apc for user objects in apache, the use of apc_store might lead to apc time bomb (the link is from 2007 but still relevant!), while apc_add mitigates this issue.

See also: https://serverfault.com/questions/342295/apc-keeps-crashing

like image 2
etov Avatar answered Sep 28 '22 14:09

etov


you need apc_add() in case you like to implement an atomic counter in a concurrent environment. thats the main use-case IMO.

like image 1
staabm Avatar answered Sep 28 '22 13:09

staabm