PHP In Memory Classes or Variable - php

Hi is there anyway to store variable into memory until server restart or manually recreate the same variable using PHP.
Actually the problem is this I nee do have a router in my application which have several configuration to route the URLs on different APIs. I do not want to load my configuration each time a request lands so I am searching a way to keep the configuration data into memory until I manually recreate the same.
Please suggest if there is anyway to do this.
Thanks in Advanced.

PHP stores all variables in memory until the process exits (if the variable is a global) or until the scope ends (if the variable is local).
I think where you're getting confused is that Apache (or whatever webserver you're using) runs continuously, but it only starts PHP for every request, so PHP is never running continuously.
To store things across different requests, you'll need to use external persistence. You can do this with PHP sessions, a database, flat files, etc. but none of these will store the data in memory.
Even though it's external, you can use a caching server like memcached to store data in memory continuously, and then use PHP to access it through the Memcached class.

Related

Best way to store variable

I need to save the value of a variable to use on another page. What is the best way to do this?
$ _SESSION is not a possibility because it will be executed by cronjob.
apc_store seemed to me an excellent function for this but it does not seem to work anymore or needs to install extension on server to work.
Is there any native PHP alternative?
Edit:
I use Curl to get the value in a site1 and saved in a variable then it is redirected to Site2 where I want to retrieve the value of that variable. Using POST is not possible because it is a form and would conflict the data that the form needs with the data I am passing. The application does not use DB, creating one so this would be bad.
Cookies falls into the same SESSION problem (I'm using cronjob)
I think the best solution would be a txt file manipulated by file_put_contents.
Would that be the only solution?
apc_store seemed to me an excellent function for this but it does not seem to work anymore or needs to install extension on server to work.
The APC variable cache is not suitable for this application -- it isn't shared between the web server and CLI scripts, and values stored in the cache may be evicted at any time.
If you need a value to be reliably stored across executions of a CLI script, write it to a file or store it in a database.

Store PHP superglobal variable until Apache server restarts

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?
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.

What are the alternative to PHP session variables?

I am writing a brand new website and I'd like to make sure that it scales out easily if I ever get to the point where I must host the site on multiple machines with a load balancer.
The user of the website can be authenticated. In other words, I need to maintain some state information. My first reflex was to use Session variables but then I am going to be limited to a single machine. I know that there are ways to store the session variables outside (in a DB, redis, memcached) but is that the only options?
What are the alternative to session variable? How Facebook and other big web sites are doing this?
P.S. I am not looking for another session handler (DB, redis, etc.). I'd like to know if there a way to completely get rid of session variables.
Ever heard of session_set_save_handler? It allows you to use mechanisms other than the default PHP session handler (the one that writes sess_xxxxxxxxxxxx files in tmp directory).
You can write your own session handler that uses a database. This could be a time consuming task; so you can stick with the default PHP session handlers for the time being and transparently switch to database when you are ready. You probably won't have to rewrite any code except implementing and plugging in your version of the six session handling functions.
You can look into caching, i.e using Zend cache or APC cache, for example.

PHP: load set of properties (from config files) only ONCE for all user requests

I have a file which contains application specific properties (e.g. timeouts etc.) which I have placed in a my_app.ini properties file and which I read using parse_ini_file().
How/where should I make this code run exactly once for all my user requests (I do not want my application to read the .ini file on every request) ?
I though of putting the config file reading code in a separate PHP and including this with include_once/require_once in all pages that require access to the config variables. But:
Will this mean that the file will be
read once for every different page
(so different times since I have
multiple pages with
require_once('config_setup.php'))
?
Is require_once/include_once
executed only once across all user
requests ?
I am interested in possibly hearing other (better) ways how to do this.
PHP doesn't keep state between multiple requests. This means that standalone PHP cannot store variables in memory between requests. Sessions are saved to disk (by default), config files needs to be reparsed, etc.
You can however use extensions such as APC or Memcache/Memcached to store variables in memory. These extensions needs to be compiled/installed on the server so that their functions are available.
Parsing your INI will not be a bottleneck for your application. For simplicity's sake, it's easier just to parse it everytime. That 0.00001 ms you'll be saving otherwise isn't worth it.
include_once() and require_once() has nothing to do with persistance. It just tells PHP to only include the script if it wasn't already included during this request.
include/require_once mean that the file will only be loaded once during the processing of a single request; however, whenever a new request is received, it will be read again as the script is parsed.
In addition, for what you want to do here, you would have to read the configuration and place its value in the session; then, if the specific key you use in the session array is defined, you do not need to reparse the file:
if ( !array_key_exists( 'config' , $_SESSION ) ) {
$_SESSION['config'] = parse_ini_file ( 'whatever.ini' );
}
However, this approach has a major disadvantage - if the configuration file changes, users that are already on the site will keep on using the previous version of the file.
First of all, reading the *.ini is hardly going to be the bottleneck of any service in normal circumstances, so really think whether the advantages you seek (which are...?) outweigh the disadvantages. I have no problem scanning INI files, although I usually use native PHP to do some settings as long as no non-PHP programmer needs to reach them. Of course, people are now going to advocate key/value stores like APC & Memcached because of the persistence issue, but that will only be an advantage if your file is (relatively) huge for an ini file, otherwise it's just overhead if you don't use it already.
The only way I know how to get a (scalar) setting across requests without extensions in Apache easily is to do some SetEnv business in the host configuration (so scalar values are just read from the environment), but those are kludgy at best, and require a server reload or restart to pick up alterations.
Stay with the ini file, or possibly with a straight PHP file with settings, in which case APC could grant you a small advantage by caching the file until it changes.

Can instances of the same PHP script cross communicate?

I'm assuming that for every page request, the webserver (eg. Apache) creates a new instance of a script in memory. Can these instances communicate with each other while running? and pass data too?
If you want to pass data between scripts in PHP I suggest using either memcached or a database. Or possibly APC.
If the scripts belong to the same session, they could theoretically communicate via the session but this would be effectively a one-way communication in most cases because only one script can access the session at any one time (session_start() locks the session until that script ends the session implicitly or explicitly).
I believe Martin and Cletus' suggestions are valid. My choice would be function of the end goal of the script.
How much data will you be throwing around? Can you handle the overhead of an external process?
What kind of data are you exchanging? Is it normalized? Or is it now worth normalizing?
Will you need to refer to that data later on? Or can it be discarded after being processed?
Will those scripts ever run on different servers?
Flat files, with a locking mechanism
Relational DB
Document DB (key/value store, whether persistent or not)
Shared memory (APC, or core functions)
Message queues (Active MQ and company)
I think you'll get the most value by externalizing the process as you can have more than one machine managing the messages/data and more than one producing/consuming them.
The model that PHP scripts operate off of doesn't really contain the notion of any kind of persistence in memory for those scripts, since generally they're designed to only run for the minimum of time required to serve the requested page. This would make it hard to have any meaningful use for stateful communication between those scripts, since typically once the page is served there's nothing more for the script to do. Thus usually any communication between PHP scripts is done more through manipulation of database entries and the like.
If you have some sort of continual processing that should be happening for which you'd want to be passing data around, you might want to look into other web application models such as servlets.
You should be able to do this with some shared memory, as described here: http://blog.taragana.com/index.php/archive/how-to-use-shared-memory-in-php/ (assuming you're not running on Windows)

Categories