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 7398613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:53:30+00:00 2026-05-29T03:53:30+00:00

I have never used memcache before now so please excuse my inexperience. Although it

  • 0

I have never used memcache before now so please excuse my inexperience. Although it is pretty self explanatory, I would like to make sure I am using the built in functions correctly as I am creating a class that will be used commercially so it must be correctly coded and efficient.

I have several questions but as they are very basic I felt it would be alright to combine them into one Stackoverflow question.

If they require an essay answer, please dont bother and I will post it up as a separate question

  1. When would I need to use memcache::addServer and what is the difference between this and memcache::connect?
  2. Does memcache overwrite stored values if it runs out of memory, even if the item has not yet expired?
  3. What would I use memcache::getExtendedStats for?
  4. How do I check to see if a connection to memcache already exists and if not, create a connection?
  5. If I have my usual memcache server of ‘localhost’ set up, how would I go about setting up another memcache server on my same dedicated server?
  6. Apart from more memory, what is the benefit of having more than one memcache server?
  7. Should I check for memcache server updates regularly?
  8. Does it use a lot of memory to run memcache::connect at the beginning of each page, even if I am not using it?
  9. When am I likely to return errors and how do I catch these?
  10. Most importantly, if I am using memcache within another class that has several methods that may be called more then once per script, how should I go about initialising the object and connecting to the server within each method?

My guess for the last question would be to do it like so:

class test {
     public function blah(){
          // Make sure the memcache object is accessible
          global $memcache;

          // Do something ...
          // Save result in memcache
          $memcache->set(...);
     }
     public function foo(){
          // Do something ...
          // No use for memcache
     }
}

// Initialise each class
$test = new test;
$memcache = new memcache;
$memcache->connect(...);

// Call some methods from the test class
$test->blah();
$test->foo();
$test->blah();

As you can see in the above example, I connect to the memcache server at the beginning of the script. If I was to include this at the beginning of every page, even on pages that do not use memcache, would this increase the response time a lot or minimal amounts? Hence, question 8!

  • 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-29T03:53:31+00:00Added an answer on May 29, 2026 at 3:53 am

    You might need some coffee or something before you read this:

    1. You’d want to use Memcache::addServer when you need to add more Memcached servers. For example, if you had a really busy website or web app… you’d probably want to have more than one Memcached server running1. Memcache::connect is used when you want to start a connection to one of your Memcached servers. Also, according to the Memcache::addServer docs, another difference between Memcache::addServer and Memcache::connect is that with Memcache::addServer, the connection is not established until actually needed2.

    2. If Memcached runs out of RAM, it will discard the oldest values3.

    3. Memcache::getExtendedStats is used to check information about your Memcached server. For example, if you need to find out how long the server has been up (uptime,) how many connections the server has, or general server usage4, this is a great tool.

    4. Probably the easiest way to check if a connection to Memcached already exists is to check your $memcache connection variable to see if it returns TRUE5. If you need to have a persistent connection (that keeps on going even after your script ends,) there is the option to use Memcache::pconnect6.

    5. If you want to have two Memcached servers going on… and your first server is already your localhost, you will most likely need to have a separate, distinct server for the second7.

    6. At least one other benefit of having more than one Memcached server is the idea that whenever you diversify your data (or servers,) even when one server goes down… you still have however many other servers there to pick up the pieces. Memcached looks8 like it is distributed over however many servers you have running… so if a server goes down, you are still losing that part of the cache. But, you do still have other servers up and running to help keep going.

    7. In general, it’s not a bad idea to keep almost any type of software up to date. It looks like Memcached is still a highly active project9 so you may want to update it when you can. But the essence of Memcached doesn’t seem to change a whole lot over past versions… so, it might not be as critical to update it compared to something like operating system software.

    8. It sounds like the way that Memcached allocates memory for TCP connections (when you make a call to your Memcached server via Memcache::connect,) does end up costing you memory10. If you are sure you aren’t going to need that connection on some of your pages, you may want to avoid making that connect call.

    9. Hard to say what type of errors might come up in your code. But, with something like Memcached, you may find errors coming up when you are running out of memory11.

    10. Like the answer to question eight, I would still recommend trying to only call that $memcache->connect() in areas where you absolutely need it. You might be using Memcached in a lot of your application or scripts; but there still will probably be places where you won’t need it.

    As far as your code idea for question 10 goes, it’s really up to you as far as the implementation goes. In general, it’s good to try to avoid global variables12 when possible, though. Instead, like that article (12) in the footnote talks about, it’s easier to just use a singleton class call for a connection… and then just call that each time you want to make a connection.

    Wow, my eyes are tired. I hope this helps, man…!

    1 http://en.wikipedia.org/wiki/Memcached (see Architecture section)

    2 http://www.php.net/manual/en/memcache.addserver.php

    3 http://en.wikipedia.org/wiki/Memcached (see Architecture section)

    4 http://www.php.net/manual/en/memcache.getextendedstats.php

    5 http://www.php.net/manual/en/memcache.connect.php (see Return Values section)

    6 http://www.php.net/manual/en/memcache.pconnect.php

    7 http://www.php.net/manual/en/memcache.addserver.php#101194

    8 Benefits of multiple memcached instances

    9 http://code.google.com/p/memcached/

    10 http://www.facebook.com/note.php?note_id=39391378919 (from Facebook’s point of view)

    11 http://groups.google.com/group/memcached/browse_thread/thread/9ce1e2691efb283b

    12 How to avoid using PHP global objects?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never used CRON or anything like that, rails etc.. before, but I
I have never used a Transaction, Commit and Rollback before and now I need
I have never used NSPredicate before so please bear with me. I have an
I have never used data context in xPages and would like to know the
I have never used Prototype before. But now when I'm using Rails, it seems
Just trying to understand that - I have never used it before. How is
I have never used enums before i have a problem while creating enums i
I have never used DotNetNuke before. I'm thinking about giving it a try to
I have never used Watin before today. I need to get a collection of
I have never used dll's before(absolutely no experience) and I wanted to replace a

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.