Memcache and Python, getting started

Posted by David in Open Source, industry, memcache, performance, python
Tuesday, November 3rd, 2009 at 15:57
python-logo

If you have ever used Python and Memcached, you probably have been looking for a library to connect to your Memcached server. And I’m sure you stumbled on the Tummy.com library which has basically no official online documentation.

Code is the best documentation:
That’s right… download the tar.gz file, go to the folder where you downloaded it, extract the content of the archive file, open your favorite editor and read the comments of the memcache.py file. It’s rough, it’s ugly, it takes time and it’s boring, but, it’s a proven way of doing it. Many nerds and geeks will tell you it’s the BEST WAY IN THE WORLD!

Sorry, I strongly disagree with that. When you install the library with the likes of apt-get (apt-get install python-memcache) or any other dependency management tool, you are lost because you have no direct code to look at (Unless you are an uber geek and decide to go look at the location of your installed files, grep for memcache and read the .py from there. Of course this is also very inconvenient and very unproductive).

Don’t use it at all:
Google for anything else? Realize that everything seems to be pointing back to that Tummy link… ohh there are a few other libraries alright, you could use the one from Twisted or maybe cmemcache which is an interesting one but yet another one that doesn’t seem to have any documentation.

Keep digging and get lucky:
You can keep digging in order to find documentation for that solution you absolutely need and then get lucky and find a link. In the case of Python memcached, if you are lucky enough you find a link on some mysql page… why? Not quite sure but it’s there.

You use it and decide to write an article about it, just like I am doing:

Here we go, Python Memcache basic usage, how to install python-memcache (on Ubuntu), how to connect to your memcache server(s), how to add a new key to the cache, how to retrieve data from memcache and how to delete (lazy delete) from memcache.

1
apt-get install python-memcache
Once everything went ok, you need to test your connection and your memcache server so simply run the python interface:

1
2
>>> import memcache
>>> s = memcache.Client([“127.0.0.1:11211”])
Of course if your memcache server is on another host and another port, you need to adjust the line above.

So now let’s test adding data to the server:

1
2
3
>>> import memcache
>>> s = memcache.Client([“127.0.0.1:11211”])
>>> s.set(“name”, “david”);
Now you have a name set in your memcache server and if you instantiate a new connection, you should be able to retrieve this “name” key.

1
2
3
>>> import memcache
>>> s = memcache.Client([“127.0.0.1:11211”])
>>> name = s.get(“name”);
You should get “david” back from that. Here’s an example on how you could actually use that in a file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import memcache
“””
Example class

This is simply an example class used for the memcache demonstration

package: echolibre.examples
“””
class Example():
“””Example of memcache tester”””
hostname = “”
server = “”
def __init__(self, hostname=”127.0.0.1″, port=”11211″):
self.hostname = “%s:%s” % (hostname, port)
self.server = memcache.Client([self.hostname])

def set(self, key, value, expiry=900):
“””
This method is used to set a new value
in the memcache server.
“””
self.server.set(key, value, expiry)

def get(self, key):
“””
This method is used to retrieve a value
from the memcache server
“””
return self.server.get(key)

def delete(self, key):
“””
This method is used to delete a value from the
memcached server. Lazy delete
“””
self.server.delete(key)

if __name__ == ‘__main__’:
sample = Example(“127.0.0.1”);
sample.set(“name”, “david”);
retrieved = sample.get(“name”);
print retrieved
So simply enough you can invoke “python example.py” or do something like

1
2
3
4
5
>>> from example import Example
>>> e = Example(“127.0.0.1”)
>>> e.set(“name”, “david”)
>>> print e.get(“name”)
>>> e.delete(“name”)
You are now ready to use memcache from your python code/classes/scripts so I hope you enjoyed this and that it’ll be useful. I’ll be posting a second article about the multi delete, multi set, and extended methods that you can’t find anywhere. In the meantime, they are mine! Mouhahahaha! (evil)

You can leave a response, or trackback from your own site.

Comments (4 Responses)
Sean Reifschneider
November 13th, 2009 at 11:08
Note, as it says on the pypi page for this package, this isn’t the “tummy.com” library, it was written by Evan Martin of Danga. I’m just doing maintenance on it.

I think you’re being a little hard on the documentation. Yes, the documentation is embedded in the code, but it’s not in comments, as you say, it’s in docstrings.

docstrings are a standard way of documenting Python code, and you can get a “man” style man page by running “pydoc memcache”.

You can also use ipython and explore the documentation while testing out the library by doing “memcache?” and creating a client connection and doing “mc?”, etc…

Thanks for writing up the article.

Sean

David
November 13th, 2009 at 11:15
Hey @sean I am hard on the documentation indeed due to the high expectations the other open source projects have been providing us 🙂

Good points about the different ways to either find documentation or generate some however I’d expect pypi to have some better documentation for packages.

That being said, your maintenance work is greatly appreciated and I love the library. There’s documentation work to be done but apart from that it’s a brilliant library if you can actually use it 🙂 It’s simple, intuitive and that’s all that matters to me after documentation 🙂

Thanks for your work on the library and I’ll make sure to rectify the fact that it is not the tummy.com library but Evan Martin’s 🙂

Sean Reifschneider
November 28th, 2009 at 08:02
I don’t know if it’ll help address your criticism, but the next release will include a “memcache.html” file built as part of the release via “pydoc -w memcache”. So, there will be a document in the release so users do not need to know about running “pydoc memcache”.

Sean

ehcache.net
January 6th, 2011 at 04:02
Memcache and Python, getting started…

If you have ever used Python and Memcached, you probably have been looking for a library to connect to your Memcached server. And I’m sure you stumbled on the Tummy.com library which has basically no official online documentation….