Search code examples
phpconfigurationmemcachedapc

Why is this page "loading 3 times?" (Is it a misconfiguration?)


  • Ubuntu 10.10
  • Apache 2.2.17
  • PHP 5.3.4
  • APC 3.1.6
  • Memcache 3.0.5

Code:

<?php
$cache = new Memcache();
$cache->connect('localhost', 11211);

$num = 1;

if (($a = $cache->get('key')) !== false) $num = $a;
$num += 1;
$cache->set('key', $num);

echo $num;

Output (on three successive refreshes, just after Apache reboot):

2

then

5

then

8

Question: Why? I'm expecting 2, 3, 4. The first page seems to check out, but subsequent page loads always increment a factor of three larger than the number I'm actually adding. In other words, if I write

$num += 10;

Then the numbers come out 11, 41, 71. What am I doing wrong? Did I mis-configure APC or Memcache? I'm fairly certain APC is the problem, but if I knew for sure what the problem was then I wouldn't be asking!

Update: The apache access_log shows 3 distinct requests for each page load when this happens.


Solution

  • what method are you using to make the request? I have observed that some of the firefox plugins (like firebug) sometimes cause the page to be loaded multiple times for some reason. It might be worth trying using some other browser, or simply wget or curl to make the request and see if it still produces the same results.