Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Memcache Questions in PHP

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!

like image 886
Ben Carey Avatar asked Feb 03 '12 17:02

Ben Carey


People also ask

Is memcache multithreaded?

Multithreaded architecture Since Memcached is multithreaded, it can make use of multiple processing cores. This means that you can handle more operations by scaling up compute capacity.

What does Memcached do in PHP?

Memcached is an object caching framework. It is essentially used to cache the database queries, making a difference in dynamic websites like Drupal and WordPress to serve pages quicker. It can moreover significantly decrease resource use on an active web server by reducing calls to the database.

Does memcache support TTL?

Adding TTL At the same time, you can and largely avoid cluttering up the cache with extra data. Time to live (TTL) is an integer value that specifies the number of seconds until the key expires. Memcached specifies this value in seconds.

What is the difference between memcache and Memcached?

They both have very basic difference while storing value. Memcache mostly considers every value as string whereas Memcached stores it value's original type.


1 Answers

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...!

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

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

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

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

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

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

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

8Benefits of multiple memcached instances

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

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

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

12How to avoid using PHP global objects?

like image 129
summea Avatar answered Sep 23 '22 00:09

summea