PHP 8.5.0 Beta 3 available for testing

Voting

: six plus two?
(Example: nine)

The Note You're Voting On

ktamas77 at gmail dot com
16 years ago
skeleton of a thread safe updater for an incremental counter:

<?php

$key
= "counter";
$value = $memcache->increment($key, 1);
if (
$value === false) {
// --- read from DB ---
$query = "SELECT value FROM database";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$db_value = $row["value"];
$add_value = $memcache->add($key, $db_value + 1, 0, 0);
if (
$add_value === false) {
$value = $memcache->increment($key, 1)
if (
$value === false) {
error_log ("counter update failed.");
}
} else {
$value = $db_value + 1;
}
}

// --- display counter value ---
echo $value;

?>

<< Back to user notes page

To Top