21

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?

Gerben Jacobs
  • 4,515
  • 3
  • 34
  • 56

4 Answers4

43

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 ?

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • A result of this difference might have major implications in concurrent environments: excessive locking, resulting with extremely high latencies or crashes (I've added another answer relating to this) – etov Nov 05 '14 at 09:47
2

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

Community
  • 1
  • 1
etov
  • 2,972
  • 2
  • 22
  • 36
1

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

staabm
  • 1,535
  • 22
  • 20
1

Really depends on the logic you need to apply: do you need to overwrite the key or not? It’s all up to you.

TRiG
  • 10,148
  • 7
  • 57
  • 107
Poelinca Dorin
  • 9,577
  • 2
  • 39
  • 43