5

I am implementing caching for my Python application and I want to use memcached. Which module do you suggest me to use? There are so many that I don't know which one to choose.

Thanks, Boda Cydo.

bodacydo
  • 75,521
  • 93
  • 229
  • 319

3 Answers3

6

I use python-memcached and there's some good advice on usage in the source code header, referenced in this answer.

Use the third parameter to set the expiry.

From bundled memcached.html help file:

set(self, key, val, time=0, min_compress_len=0)

so

mc.set(key, val, time)

More info and exmaples here

Community
  • 1
  • 1
Andy
  • 17,423
  • 9
  • 52
  • 69
  • Thanks! But do you know how to make a key expire? The examples on that page, just set the keys, but how to make them expire after, let's say 444 seconds? – bodacydo Mar 26 '10 at 16:37
3

I use cmemcache, which is more performant (but no more mantained). As its developer suggest, you can switch to http://code.google.com/p/python-libmemcached.

S.c.
  • 85
  • 1
  • 4
2

I use python-memcache because:

  1. You can run in locally
  2. It is embedded in Django framework
  3. Simple to use

from memcached.py header:

    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")

or use as part of Django framework: (details here)

>>> from django.core.cache import cache
>>> cache.set('my_key', 'hello, world!', 30)
>>> cache.get('my_key')
'hello, world!'
Denis Kanygin
  • 1,125
  • 12
  • 15