In ColdFusion, there is a "server" variable scope which allows you to store variables in the ColdFusion memory space. These variables persist between page requests. Its good for stuff that applies to the entire server and across all of your applications.

In my PHP scripts, I want to have a variable that is accessable by all pages and scripts on the entire server. Can someone please provide me with an example or link to information on how to accomplish this?

I see in PHP, there is a $SERVER[] construct, but that does not appear to persist data in memory between pages. For example, say I wanted to keep a variable in memory called $newestMemberName which would remember the user name of the most recent user to join my site? I could use a database or filesystem, but I want to use the memory to store this data for speed reasons.

Thanks,
Jon

    I'd suggest just using a database. If the data is frequently accessed, it will probably end up cached in memory anyway.

      You have greater control over shared memory. Also, you can perform upkeep on the shared memory using a cron job or whatever, preloading it for expected use. I'm sure the average user wouldn't have many issues with that, though.

        This might sound crazy but databases are too slow for what I'm doing. Thanks though. What I ended up doing here was storing the data in text files in the file system. That is fast enough and it wasn't as involved in using shared memory. Does anybody know of any custom functions available out there that simplify the shared memory stuff? Its a little lower level than I wanted to get.

        Thank you!

          Your database was too slow? That suggests there's something wrong with your database, as it should be plenty fast for everything but the largest tasks 🙂

          The shared memory functions are quite simple, once you get past the hexadecimal notation they use. Once you get it set up, you can use it in a similar manner to a file.

          Stick with the shared memory thing - it's very, very fast.

            I'm working on code for an advertising server that gets hundreds of requests per second and the SQL procedure was taking too long... plus I'm running into database connection and thread problems so its easier and faster to use the filesystem...

              I can see how that could be a big problem. I worked on a site of that size, and we ended up getting the MySQL VP of training over from Finland (to London) just to go through with us how to optimize our databases :-P pretty hardcore 😉

              Shared memory is definitely, without a shadow of a doubt, the best way for you to deal with this. Memory is the fastest storage you have in your PC. True, saving it to a flat file means you'll probably get it cached, but when you're dealing with hundreds of hits a second, probably isn't good enough. If you use shared memory, you can control with 100% accuracy what's contained in there at any one time.

              If you want a good coding challenge, you could write some small functions to simplify the shared memory access. OO would be the ideal 😉

              dave

                Write a Reply...