How to get started with Memcache and Python – Beginners Guide

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)

Share:

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn

Leave a comment

Your email address will not be published.

Social Media

Most Popular

Get The Latest Updates

Subscribe To Our Weekly Newsletter

No spam, notifications only about new products, updates.

Categories

On Key

Related Posts