Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 756911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:18:01+00:00 2026-05-14T15:18:01+00:00

The title of this question isn’t so clear, but the code and question is

  • 0

The title of this question isn’t so clear, but the code and question is straightforward.

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.

I can do this 2 ways:

//version a
$key='OPD_'.date('Ymd').'_'.$type.'_'.$user;
if($memcache->get($key)===false){
 $memcache->set($key,'1',false,$expire);
 //show ad
}

//version b
$key='OPD_'.date('Ymd').'_'.$type.'_'.$user;
if($memcache->add($key,'1',false,$expire)){
 //show ad
}

Now, it might seem obvious that b is better, it always makes 1 memcache call. However, what is the overhead of “add” vs. “get”? 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:

a: (5 get * 1 effort) + (1 set * 5 effort) = 10 units of effort

b: (5 add * 5 effort) = 25 units of effort

Would it make sense to always do the add call? Is this an unnecessary micro-optimization?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:18:02+00:00Added an answer on May 14, 2026 at 3:18 pm

    Here’s some quick and dirty code I whipped up to test this, if anyone is interested:

    <?php
    require('include.php');
    $memcache = new Memcache();
    
    foreach(Config::$CONFIG['memcache_server'] as $memcache_server){
        $memcache->addServer($memcache_server,11211,false);
    }
    
    $iterations = 300;
    $max_pages_per_visit = 25;
    
    $time_now = microtime(true);
    for($pages_per_visit = 1; $pages_per_visit<=$max_pages_per_visit; $pages_per_visit++){
        foreach(array('gs','a') as $method){
            $start = microtime(true);
            for($x = 0; $x < $iterations; $x++){
                $key = 'testmc'.$time_now.'_'.$pages_per_visit.'_'.$method.'_'.$x;
                switch($method){
                    case 'gs':
                        for($y = 0 ; $y < $pages_per_visit; $y++){
                            if($memcache->get($key)===false){
                                $memcache->set($key,'1',null,5);
                            }
                        }
                        break;
                    case 'a':
                        for($y = 0 ; $y < $pages_per_visit; $y++){
                            $memcache->add($key,'1',null,5);
                        }
                        break;
                }
            }
            $end = microtime(true);
            $results[$pages_per_visit][$method] = $end - $start;
        }
    }
    
    //print results
    print('<pre>');
    foreach($results as $pages_per_visit => $data){
        $speed_diff = $data['gs'] - $data['a'];
        $speed_percentage = round($speed_diff / $data['gs'] * 100,2);
        echo($pages_per_visit.' pages : add is faster by :'.$speed_diff.' ('.$speed_percentage.')%'.PHP_EOL);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(The title for this question isn't the best, but I'm unsure how else to
Question is in the title. I'd just love to understand why this isn't supported?
(Really strugging to title this question, so if anyone has suggestions feel free.) Say
I'm not really sure how to title this question but basically I have an
i didn't really know how to title this question, but here's a thing that
See the title of this question. I want to play with the exception raised
I have no good title for this question, but my problem is to set
Alright, hard to phrase an exact title for this question, but here goes... I
The title of this question is a bit misleading, but I couldn't summarize this
OK, the question title probably isn't the best, but I'm looking for a good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.