tags:

views:

81

answers:

1

Hi all!

I've made this question: http://stackoverflow.com/questions/2921469/php-mutual-exclusion-mutex

As said there, I want several sources to send their stats once in a while, and these stats will be showed at the website's main page.
My problem is that I want this to be done in an atomic manner, so no update of the stats will overlap another one running in the background.

Now, I came up with this solution and I want you PHP experts to judge it.

stats.php

<?php
define("my_counter", 12);
?>

index.php

<?php
include "stats.php";

echo constant("my_counter");
?>

update.php

<?php
$old_error_reporting = error_reporting(0);

include "stats.php";

define("my_stats_template",'
<?php
define("my_counter", %d);
?>
');

$fd = fopen("stats.php", "w+");
if($fd)
{
    if (flock($fd, LOCK_EX))
    {
        $my_counter = 0;

        try
        {
            $my_counter = constant("my_counter");
        }
        catch(Exception $e) { }

        $my_counter++;

        $new_stats = sprintf(constant("my_stats_template"), $my_counter);

        echo "Counter should stand at $my_counter";
        fwrite($fd, $new_stats);
    }
    flock($fd, LOCK_UN);
    fclose($fd);
}

error_reporting($old_error_reporting);
?>

Several clients will call the "update.php" file once every 60sec each. The "index.php" is going to use the "stats.php" file all the time as you can see.

What's your opinion?

A: 

You may find a solution like Memcache may be more intuitive, faster, and useful than rewriting a PHP file every time you want to update the value.

Then there's the idea of stroing it in a database, yes I know this is slow but it's persistent.

Last, rather than writing PHP to a file why not write the value itself to a shared file and just read it? This will work nicely with your locking mechanism and won't require writing 'valid' PHP to a file which just strikes me as wrong.

Matt S
The logic beind doing it the way I did is this: An included PHP file is being cached on the PHP parser's memory, not to mention that using constants is even faster in terms of parsing (string to number). So, if I'll do it in your way, i.e write to a shared file and then read everytime I need it (and I'll need it alot, with every page visitor hit), I'll hit the hard-disk much more times than having it cached at PHP's memory. Am I wrong?
Poni
As far as for your other solutions (memcached / database): It involves a whole bunch of software and maybe even hardware. Additionally, it involves networking (queries/responses) which takes time. Keep in mind that I'll have much more readings than writings to the stats.php file.
Poni
Memcache offers several performance gains the network overhead is minimal. It also scales extremely well. You're going to have to backup the statement that PHP caches the file included via a require_once before I even begin to believe that. I still firmly believe the way you're doing this is incorrect. It sounds like you have a single server hosting your pages which means you don't need distributed cache. Look into APC (http://php.net/manual/en/book.apc.php) this will likely give you the best performance for your situation. Writing PHP to a file which you load is just a hot bed for problems.
Matt S