Linux high-speed(/no impact) storage (for settings) and PHP

114 views Asked by At

I'm sorry if the title sounds confusing, if you get any better ideas after this description, feel free to suggest.

In short, I'm using PHP on Linux with the following hypothetical files/code:

job.php:

if(setting_get('started')!='on'){
  setting_set('started','on');
  echo 'Starting...';
  set_time_limit(0); // ad infinitum (in theory)
  ignore_user_abort(true); // ignore disconnection
  while(setting_get('started')=='on'){
    // do something
    sleep(10);
  }
}else echo 'Already started!';

interface.php:

switch($_REQUEST['action']){
  case 'status':
    echo 'Service is: '.setting_get('on');
    break;
  case 'start':
    header('Location: job.php');
    break;
  case 'stop':
    setting_set('started','off');
    break;
  case 'restart':
    setting_set('started','off');
    header('Location: job.php');
    break;
}
echo '<a href="?action=status">Check Status</a>';
echo '<a href="?action=start">Start</a>';
echo '<a href="?action=stop">Stop</a>';
echo '<a href="?action=restart">Restart</a>';

The code should be quite self-explanatory. Basically, job.php should be able to run only a single instance, indefinitely until the user specifically stops it from interface.php, which is there to mediate between the user and the job.

My problem is setting_get and setting_set - they're just hypothetical functions which can be easily replaced with a flatfile or a database. The thing is that they're called a lot which is why they should run fast as well consume little memory (if possible, even none at all).

Ideas?

Edit: It should be noted that the settings I'm saving so far could as well be a simple boolean flag.

Edit2 With regards to Memcached / APC / Redis. I was wondering, couldn't I use the normal mysql functions over a DB of type MEMORY? I wonder how much overhead there is? Thing is, I'd rather use something I've already got than having to install new stuff, but then again, it depends on implementation.

2

There are 2 answers

3
Hamish On

How about APC's apc_store and apc_fetch?

1
vissi On

set_time_limit(0); // ad infinitum (in theory)

you can run your job in cron without such limit.

Use shared memory (fastest), memcached, redis (given in order of speed).