Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Python Memcached2

A new 100% Python memcache client library. This is targeted at Python 3.3+ and Python 2.7, though it may be possible for it to work with Python 2.6 as well.

I am looking for feedback on this module, its design and architecture.

2013-09-04: I'm tagging release-0.3, so that I can make some dramatic changes to the server selection code. Until further notice, you probably want to use the 0.3 tag rather than HEAD.

These "What's new" dated entries are now recorded in the "docs/whatsnew.rst" file.


The dictionary/mapping interface looks like this:

>>> import memcached2
>>> mcd = memcached2.ExceptionsAreMissesMapping(('memcached://localhost/',))
>>> 'foo' in mcd
>>> mcd['foo'] = 'hello'
>>> 'foo' in mcd
>>> mcd['foo']
>>> len(mcd)
>>> del(mcd['foo'])
>>> len(mcd)

The lower-level :py:class:~memcached2.Memcache interface raises exceptions in the cases of unexpected results or server connection issues. Here's a small example, many more examples are available in the documentation.

>>> import memcached2
>>> memcache = memcached2.Memcache(('memcached://localhost/',))
>>> try:
>>>     data = memcache.get('session', data)
>>> except Memcached2.RetrieveException:
>>>     data = read_session_from_database()
>>> try:
>>>     memcache.set('session', data)
>>> except Memcached2.StoretrieveException:
>>>     pass


The documentation, including extensive examples, is available at python-memcached2 on Read The Docs

Helping Out

I'd really appreciate review of the design and implementation, and in particular how it deals with a threaded environment.

I do need to document my current thoughts on the architecture, but here are my thoughts about it:

I'd also like to have a comprehensive test suite for it. The old package had a basic test suite, but it was missing a lot of test cases. I'd like ot have a test case that demonstrates the failure before looking at a patch to fix it.

Thoughts? If you know anyone who is interested in Python and Memcached, please direct them to this project, I'd like the input early.

The previous module uses "readline()", which does a system call for every character in the input. This is a lot of overhead, so I'm switching to a buffered input wrapper and using recv() to read large blocks of data, then search for the newline or other data. Should be a big performance boost, especially on large data stores.


python-memcached2 is licensed under version 2.0 of the Apache License. See the file "LICENSE" in the top level directory or the Copyright notice in the software.


The existing python-memcached module has a long history, but also is a very complicated set of code that I did not write and has very few tests. It was written long long ago, and so I've felt that there was a benefit both to starting over from a clean slate, using lessons learned, and to using more modern software-development practices. Plus, it's a good opportunity for supporting Python 3 and Python 2 (though there is a Python 3 patch for python-memcached).

The current code is deployed widely, so doing extensive changes to it is fraught with peril.

So in early April I began experimenting with a new code-base for python-memcached. I've been exploring a few different things to see where they go, and so far I think it looks promising.


A 100% python memcached client library.




No packages published


You can’t perform that action at this time.