Skip to content

Instantly share code, notes, and snippets.

@martg0
Created February 11, 2010 15:52
Show Gist options
  • Save martg0/301638 to your computer and use it in GitHub Desktop.
Save martg0/301638 to your computer and use it in GitHub Desktop.
memcache Example
"""
See U{the MemCached homepage<http://www.danga.com/memcached>} for more about memcached.
"""
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment