title: memcached: which is faster, doing an add (and checking result), or doing a get (and set when returning false)
body: <p>The title of this question isn't so clear, but the code and question is straightforward. </p>
  
  <p>Let's say I want to show my users an ad once per day. To accomplish this, every time they visit a page on my site, I check to see if a certain memcache key has any data stored on it. If so, don't show an ad. If not, store the value '1' in that key with an expiration of 86400.</p>
  
  <p>I can do this 2 ways:</p>
  
  <pre><code>//version a
  $key='OPD_'.date('Ymd').'_'.$type.'_'.$user;
  if($memcache-&gt;get($key)===false){
   $memcache-&gt;set($key,'1',false,$expire);
   //show ad
  }
  
  //version b
  $key='OPD_'.date('Ymd').'_'.$type.'_'.$user;
  if($memcache-&gt;add($key,'1',false,$expire)){
   //show ad
  }
  </code></pre>
  
  <p>Now, it might seem obvious that b is better, it always makes 1 memcache call. <strong>However, what is the overhead of "add" vs. "get"?</strong> These aren't the real comparisons... and I just made up these numbers, but let's say 1 add ~= 1 set ~= 5 get in terms of effort, and the average user views 5 pages a day:</p>
  
  <pre><code>a: (5 get * 1 effort) + (1 set * 5 effort) = 10 units of effort
  
  b: (5 add * 5 effort) = 25 units of effort
  </code></pre>
  
  <p>Would it make sense to always do the add call? Is this an unnecessary micro-optimization?</p>