5

I was wondering, I have this big array, is it possible to have it only once in memory rather then once per thread? Take the tags here at stackoverflow as example. They barely ever change, why not have a single memory spot for them? And maybe even keep that array permanently in memory?

j0k
  • 22,600
  • 28
  • 79
  • 90
RoboTamer
  • 3,474
  • 2
  • 39
  • 43

4 Answers4

4

Take a look at apc_store

Unlike many other mechanisms in PHP, variables stored using apc_store() will persist between requests (until the value is removed from the cache).

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • @Rudie, It is a shared memory cache. But duplication of data in threads might of course occur during use. – Captain Giraffe May 31 '11 at 11:57
  • So there's not much advantage in this case? Because the array is static anyway (I think..?) and it's used in several threads > several different memory blocks. Using APC (**or any PHP cache**) is probably slower than just defining the array somewhere in the script. Right? – Rudie May 31 '11 at 12:29
  • I would expect the opposite by a margin. A cached array using cached opcode should be significantly faster. YMMV: test your particular scenario. – Captain Giraffe May 31 '11 at 15:49
2

For this you can use the shmop functions or a dedicated memory cache like memcached.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • I don't see any advantage to use this over a shared Linux memory folder. Each process still has to load everything from shmop, so each process is adding the data to the memory again. Actually it is much easer to use the folder, in Debian that would be /dev/shm/. – RoboTamer Jun 02 '11 at 06:51
1

each php process runs isolated from others. This is different from java for example where when you have a single object you can make live until the JVM is restarted.

sadly if you have an object (small or big) it will be loaded each time the php script runs BUT if you want to share something in memory between runs you could use APC, memcache or shared memory

Between those options I highly recommend you APC.

Gabriel Sosa
  • 7,897
  • 4
  • 38
  • 48
0

No -- the synchronization issues inherent in sharing a single variable directly between PHP interpreters make that impossible. It's much more likely that Stack Overflow simply avoids "thinking about" the whole tags array at once.

You can use the variable storage functions in APC (apc_store and apc_fetch) to store serialized data in shared memory, though.