95

I'm writing a web app using Python and the web.py framework, and I need to use memcached throughout.

I've been searching the internet trying to find some good documentation on the python-memcached module, but all I could find was this example on the MySQL website, and the documentation on its methods isn't great.

Jonathan Prior
  • 6,114
  • 7
  • 29
  • 26

3 Answers3

148

It's fairly simple. You write values using keys and expiry times. You get values using keys. You can expire keys from the system.

Most clients follow the same rules. You can read the generic instructions and best practices on the memcached homepage.

If you really want to dig into it, I'd look at the source. Here's the header comment:

"""
client module for memcached (memory cache daemon)

Overview
========

See U{the MemCached homepage<http://www.danga.com/memcached>} for more about memcached.

Usage summary
=============

This should give you a feel for how this module operates::

    import memcache
    mc = memcache.Client(['127.0.0.1:11211'], debug=0)

    mc.set("some_key", "Some value")
    value = mc.get("some_key")

    mc.set("another_key", 3)
    mc.delete("another_key")

    mc.set("key", "1")   # note that the key used for incr/decr must be a string.
    mc.incr("key")
    mc.decr("key")

The standard way to use memcache with a database is like this::

    key = derive_key(obj)
    obj = mc.get(key)
    if not obj:
        obj = backend_api.get(...)
        mc.set(key, obj)

    # we now have obj, and future passes through this code
    # will use the object from the cache.

Detailed Documentation
======================

More detailed documentation is available in the L{Client} class.
"""
John Douthat
  • 40,711
  • 10
  • 69
  • 66
Oli
  • 235,628
  • 64
  • 220
  • 299
  • Thanks, the source code comments are very helpful. – Jonathan Prior May 15 '09 at 13:49
  • I can't understand what 'mc' is. Can you please explain? – bodacydo Mar 26 '10 at 17:49
  • 9
    `mc` is the Memcache Client object, it represents the memcached connection. – moshen Aug 13 '10 at 15:11
  • `import memcache` where's memchche? – Scott 混合理论 Nov 13 '12 at 10:30
  • 4
    @Kevin混合理论 This whole question is about [python-memcached](http://www.tummy.com/Community/software/python-memcached/). That's what provides `memcache`. – Oli Nov 13 '12 at 12:09
  • What are incr and decr methods achieving here? – VaidAbhishek Sep 03 '13 at 06:40
  • @VaidAbhishek They're shortcuts for incrementing integer so that instead of having to get, increase and then set, you can do it all in a single write-only operation. – Oli Sep 03 '13 at 07:11
  • @Oli Thanks for reply. But why do you need to increment a counter on a cached object. I would assume that a cached object (value) is cached for a certain interval, after which it should get deleted automatically. But why maintain some integer data with cache entries. – VaidAbhishek Sep 03 '13 at 08:09
  • @VaidAbhishek Don't forget MC is distributed too. Having an atomic counter between multiple threads or even servers is useful for keeping track of things. – Oli Sep 03 '13 at 08:31
  • `mc.set("key", "1")` should be `mc.set("key", 1)` – themiurgo Oct 01 '13 at 10:31
  • 1
    @themiurgo the code above is [a comment in the header of the actual python-memcached code](http://bazaar.launchpad.net/~python-memcached-team/python-memcached/trunk/view/head:/memcache.py). That's how it was in 2009 and that's how it still is today. Comments throughout still say "it must be the string representation of an integer". If you think that's wrong, file a bug with them to get them to update their documentation. – Oli Oct 01 '13 at 11:21
43

I would advise you to use pylibmc instead.

It can act as a drop-in replacement of python-memcache, but a lot faster(as it's written in C). And you can find handy documentation for it here.

And to the question, as pylibmc just acts as a drop-in replacement, you can still refer to documentations of pylibmc for your python-memcache programming.

Felix Yan
  • 14,841
  • 7
  • 48
  • 61
  • 3
    Note that `pylibmc` does not work on Python 3. – jbg Jul 14 '14 at 04:48
  • 2
    While true, `python-memcached` doesn't support Python 3 either. pylibmc is currently preparing for a release with Python 3 support – anthonyryan1 Nov 05 '14 at 18:52
  • 11
    Both of them now support Python3. – Aidin Jun 20 '16 at 18:26
  • 1
    Just a note on installing: `apt-get install libmemcached-dev` and then `pip install pylibmc` – Christian Jul 01 '16 at 20:08
  • Problem for me was that pylibmc requires build-tools on linux and is difficult to install on windows. I use mixed win/lin environment so I switched back to python-memcached for compatibility reasons. Biggest problem was the policy I have against installing build-tools on linux production servers. Speed difference between python-memcached and pylibmc should almost never be an issue. – Cris Oct 10 '18 at 18:43
8

A good rule of thumb: use the built-in help system in Python. Example below...

jdoe@server:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import memcache
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'memcache']
>>> help(memcache)

------------------------------------------
NAME
    memcache - client module for memcached (memory cache daemon)

FILE
    /usr/lib/python2.7/dist-packages/memcache.py

MODULE DOCS
    http://docs.python.org/library/memcache

DESCRIPTION
    Overview
    ========

    See U{the MemCached homepage<http://www.danga.com/memcached>} for more about memcached.

    Usage summary
    =============
...
------------------------------------------
Yarnix
  • 97
  • 1
  • 1
  • This is no longer valid. 2.7.3 doesn't ship with a memcache module installed by default, and the link to documentation is broken as well. – iandouglas Dec 26 '12 at 18:52
  • 1
    @iandouglas: what you write is true for my debian 6.0.7, but I only had to `apt-get install python-memcache` to get the module. – jfg956 May 21 '13 at 10:14