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.

thanksyo
  • 839

10 Answers10

58

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.

Nate
  • 2,386
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.

David Pashley
  • 23,963
25

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.

24

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.

tylerl
  • 15,245
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

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.

Spechal
  • 751
-1

php-memcached uses libmemcached, another is not. That's all. Same usage, same functions, same classes, same features. No performance problem no side effect etc.

-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
Simon B.
  • 123