76

I want to use memcached

http://www.danga.com/memcached/

I have installed it through yum install memcached

But now I need to connect to PHP, and there is an extension named memcache and one named memcached? ARGH

https://www.php.net/manual/en/book.memcache.php
https://www.php.net/manual/en/book.memcached.php

Could someone point me in the right direction here.. which one is going to work?

Also, do I need to open any ports for it to work even though it's local? After running it, I try telnet 127.0.0.1 11211 and I get connection refused.

3
  • 28
    If you're looking at this question now, please bear in mind that the accepted answer is wrong. Picking a solution based solely on the name is dangerous, particularly when the one with the better name is a worse project. Please look at the other answers for more information.
    – tylerl
    Oct 7, 2010 at 23:34
  • @thanksyo please uncheck the answer you checked. That will just confuse more people coming here late. In the meantime, I downvoted the incorrect answer and suggest others do the same until thanksyo gets around to unchecking it.
    – joedevon
    Sep 6, 2013 at 20:07
  • Also stackoverflow.com/q/1442411/632951
    – Pacerier
    Jan 19, 2015 at 8:36

9 Answers 9

26

The short answer: Either one is what you are looking for, but my first choice would be memcache (the first one you listed), purely based on its correct use of nomenclature.

Now here's how I came to that conclusion:

Here is a quick backgrounder in naming conventions (for those unfamiliar), which explains the frustration by the question asker: For many *nix applications, the piece that does the backend work is called a "daemon" (think "service" in Windows-land), while the interface or client application is what you use to control or access the daemon. The daemon is most often named the same as the client, with the letter "d" appended to it. For example "imap" would be a client that connects to the "imapd" daemon.

This naming convention is clearly being adhered to by memcache when you read the introduction to the memcache module (notice the distinction between memcache and memcached in this excerpt):

Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

The Memcache module also provides a session handler (memcache).

More information about memcached can be found at » http://www.danga.com/memcached/.

The frustration here is caused by the author of the PHP extension which was badly named memcached, since it shares the same name as the actual daemon called memcached. Notice also that in the introduction to memcached (the php module), it makes mention of libmemcached, which is the shared library (or API) that is used by the module to access the memcached daemon:

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

This extension uses libmemcached library to provide API for communicating with memcached servers. It also provides a session handler (memcached).

Information about libmemcached can be found at » http://tangent.org/552/libmemcached.html.

In summary, both are functionally the same, but they simply have different authors, and the one is simply named more appropriately than the other.

10
  • 74
    Is the name of the project really an appropriate reason to pick one implementation over the other? Sep 8, 2009 at 17:41
  • 4
    David: Not quite... but it's certainly enough of a reason to leave a particular implementation until last in my testing, thereby effectively giving preference to the other(s). I would most likely still try out all implementations available, but technical flaws in descriptions of software by the author tend to make me wonder about what technical flaws may lie in the software. I did read your comment, though, about the actual software, so I gave you an upvote for your answer too. :-) Sep 8, 2009 at 18:33
  • 4
    I never thought the "d" in memcached to mean deamon but to mean the past participle of the verb cache. Plus, you couldn't name the new interface as "memcache" since there was already one with that name.
    – GetFree
    May 16, 2010 at 23:24
  • 9
    The amount of bugs and whether or not the extension is actively developed is so much more important than being anal about the problematic d. Seriously, you should not even think about testing one extension last because of something like that, it's completely off the bat
    – Fake51
    Jun 26, 2010 at 10:50
  • 13
    Erm, both aren't functionally the same: memcached supports cas tokens for instance, memcache doesn't. If you need those, you know which (ill named or not) to pick.
    – Wrikken
    Sep 24, 2010 at 11:53
57

You probably want to see the PHP Client Comparison.

Short version: They will both work, and for most cases either one will do just fine.

Regarding the other issue: Yes, you should be able to do telnet 127.0.0.1 11211. Very few firewalls would block localhost from communicating with itself. If you are not able to connect, verify that memcached really is running by doing ps auxwww | grep memcached, which will also show you the command-line arguments used to start memcached. One of the arguments should be -p 11211 or another port number. See man memcached for the meaning of all the possible arguments.

5
  • 3
    Upvote for adding some troubleshooting steps. Sep 8, 2009 at 18:37
  • This is really the clear answer.
    – johnlemon
    Aug 12, 2013 at 8:41
  • @Nate, What's the Windows equivalent of ps auxwww | grep memcached?
    – Pacerier
    Jan 19, 2015 at 8:38
  • 1
    @Pacerier: Take a look at superuser.com/questions/415360/…. Process Explorer is the easiest way to find a running process and see its command-line arguments.
    – Nate
    Jan 19, 2015 at 20:04
  • @Pacerier that command checking if the server process is running and seeing how it was invoked.
    – Jasen
    Jan 13, 2016 at 2:49
29

As Nate's link suggests, both work perfectly well for simple usage. However, memcached supports more features that allow you to get the most performance out of memcached. The binary protocol reduces the amount of data required to be sent between client and server. Multigets and multisets allow you to get/set multiple items at the same time. If you're finding you need more oomph out of memcache, memcached is the better module. The use of libmemcached suggests that the library itself is possibly more optimised than the PHP only version.

Memcached is a more recent module compared to memcache, having only been released 8 months ago. If you need to target an older version of PHP, then you can only really use memcache.

1
  • Great explanation!
    – john
    Oct 8, 2011 at 1:58
23

The older, buggier one is called php-memcache because that seemed the most appropriate name. The newer, better version independently developed by the folks at Digg was instead named php-memcached in the interest of disambiguation.

People who would recommend you pick one over the other based solely on the correctness of the name really have no business offering technical advice.

7

Having used php-memcache recently, I'd have to point you to php-memcached.

Here's a couple of reasons off the top of my head..

1) There's no getErrorCode() or equivalent method, so if get() returns FALSE, you'll have no idea whether that's because the value stored in memcache IS false, or whether there was an issue of some sort.

2) Its hashing algorithm for consistent hashing appears to differ from other implementations, such as the many client libraries built off of libmemcached. This means that if you want to use the same memcache cluster with multiple languages, you will likely have issues where you'll store a value with the PHP client, and other clients won't find it.

3

Both have issues as of today. PECL/memcache is old and reliable, with a few exceptions where it is out dated. PECL/memcached 1.x is beta at best. Some functions don't deal with numeric keys, it leaks connections when using persistent connections. Some (all?) is being fixed in GitHub and will be released at some point in 2.x, but not today. For a more detailed review see http://brian.moonspot.net/php-memcached-issues

1
  • PECL/memcached 1.x is now stable; PECL/memcache 2.x is limited; PECL/memcache 3.x is highly unstable.
    – aredridel
    Feb 28, 2013 at 18:43
2

I'm working on a book "Expert PHP and MySQL". I wrote pretty much the same thing about the differences. What I recommended was PECL/memcached. #1 - it wraps around a full-featured well-performing C library #2 - it is more recently maintained. #3 - more features. No disparagement to PECL/memcache.

1

I've had some pretty severe issues with Drupal 6 and php-pecl-memcached in which response times were delayed by up to 5ms per call and would cause one particular site to create 5x more calls than before the change to php-pecl-memcached. Changing to php-pecl-memcache fixed all of the problems; latency and extra calls.

This answer is here solely to help those using Drupal 6 and possibly Drupal 7.

-3
  • memcache is a client of memcached (the server)
  • libmemcached is also a client to memcached

And there are other clients for memcached, see http://code.google.com/p/memcached/wiki/Clients

Cut & paste from the web:

http://www.php.net/manual/en/intro.memcache.php

  Memcache module provides handy procedural and object oriented 
  interface to memcached, highly effective caching daemon, which was 
  especially designed to decrease database load in dynamic web applications.
The Memcache module also provides a session handler (memcache).
More information about memcached can be found at » www.memcached.org 

http://www.php.net/manual/en/intro.memcached.php

   » memcached (http://www_memcached_org/) is a high-performance, distributed 
  memory object caching system, generic in nature, but intended for use in 
  speeding up dynamic web applications by alleviating database load.
  This extension uses libmemcached library to provide API for communicating with
  memcached servers. It also provides a session handler (memcached).
  Information about libmemcached can be found at 
   » tangent.org/552/libmemcached.html

http://pecl.php.net/package/memcached "PHP extension for interfacing with memcached via libmemcached library" This extension uses libmemcached library to provide API for communicating with memcached servers. https://github.com/andreiz/php-memcached

http://pecl.php.net/package/memcache "memcached extension" Memcached is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory. This extension allows you to work with memcached through handy OO and procedural interfaces.

http://code.google.com/p/memcached/wiki/PHPClientComparison

                        pecl/memcache      pecl/memcached
  First Release Date      2004-06-08         2009-01-29 (beta)
  Actively Developed?     Yes                    Yes
  External Dependency     None              libmemcached
1
  • 6
    This long-winded answer doesn't even come close to making things clear for the poster. The poster is already aware there are multiple clients. Listing them, and proposing alternative names, doesn't make it easier to pick the right one. Apr 9, 2011 at 21:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .