3

Is there a way to store a variable in server RAM using Apache 2 and PHP, holding it until the server shuts down? I need to store a very large array (hashmap) which should be initialized once (not once for user, for example only when the server starts) and accessed from different files.

$GLOBALS doesn't do the global trick (I can't access $GLOBALS variables from different files, am I doing it wrong?); $_SESSION does it, but it's not what I need. I need to store it on the server instant access memory, losing it only when the server restarts.

Obviously, the answer is not "database", since querying the database would be slower than access an array from the RAM.

It can be easily done on a Java server, why not on Apache?

5
  • 1
    global !== persistent.... if you need to "persist" variables, then there are plenty of options ranging from database and filesystem, to caches like redis or memcache, APCu, or even Apache shared memory Commented Jul 12, 2015 at 16:19
  • A global variable only is stored for the current request, and not between requests on the machine. Java servlets have server-level storage by default, I believe, but PHP does not. However it is very easy to add a feature to PHP to do this. Commented Jul 12, 2015 at 16:41
  • It doens't need to be persistent: in fact, database/filesystem is where I take the raw data from. Imagine a large dictionary: I read the file once and build the array. I can't read the file (or query the db) every time, I just need to keep the array as long as the server lives. Even to unserialize a 1GB array is too slow, and that's what memcache does (see stackoverflow.com/questions/1190662/…). I don't need the object to survive to server's death. I was looking for something like storing in $_SERVER array, it doesn't look like it should be done Commented Jul 12, 2015 at 17:08
  • Thank you halfer. Can it be done without serializing/unserializing objects? Something like a everlasting $_SESSION with the same ID for every user? I've always looked at $_SESSION as a place to store user informations, not globals. Commented Jul 12, 2015 at 17:15
  • It looks like it can't be done: bytes.com/topic/php/answers/6503-keeping-array-memory Keyword is "application level variables" and there's no such a thing in php. Looks like I'm changing language. Commented Jul 13, 2015 at 18:20

1 Answer 1

1

I've found a decent workaround using apc_store (http://php.net/manual/en/function.apc-store.php) with time to live = 0. It's not exactly what I needed (I think it still uses serialization) but it's pretty close. Way better than memcache.

Sign up to request clarification or add additional context in comments.

3 Comments

If you serve php via php-fpm then you wouldn't have the mentioned problem since the web server and php application server would be disconnected so restarting one wouldn't affect the other.
Thanks for the tip @Mjh. I'm not sure I've understood: is there a way via php-fpm to store data into the server and for the app to retrieve that data without serialization?
I confused you, so I apologize. The problem is that it's not clear what exactly you're achieving by storing the mentioned data into memory and why you need to restart the server and then fetch that data from the memory. The reason I posted the comment about php-fpm is that I'm unaware why you restart the server - so in my eyes, it's better to have php process unaffected by whatever happens to HTTP server, hence the answer is using php-fpm (not applicable if you use Windows for your server OS). However, since apc does what you want, I think we better leave it at that to avoid confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.