Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good examples of python-memcache (memcached) being used in Python? [closed]

I'm writing a web app using Python and the web.py framework, and I need to use memcached throughout.

I've been searching the internet trying to find some good documentation on the python-memcached module, but all I could find was this example on the MySQL website, and the documentation on its methods isn't great.

like image 343
Jonathan Prior Avatar asked May 15 '09 13:05

Jonathan Prior


People also ask

What is memcache in Python?

This software is a 100% Python interface to the memcached memory cache daemon. It is the client side software which allows storing values in one or more, possibly remote, memcached servers. Search google for memcached for more information. This package was originally written by Evan Martin of Danga.

What is Memcached used for?

Memcached can serve cached items in less than a millisecond, and enables you to easily and cost effectively scale for higher loads. Memcached is popular for database query results caching, session caching, web page caching, API caching, and caching of objects such as images, files, and metadata.

Can Memcached be used as a database?

keep in mind that memcache is a simple key/value storage, not a database capable of any search or join logic.

What is the difference between Redis and Memcached?

Although they are both easy to use and offer high performance, there are important differences to consider when choosing an engine. Memcached is designed for simplicity while Redis offers a rich set of features that make it effective for a wide range of use cases.


1 Answers

It's fairly simple. You write values using keys and expiry times. You get values using keys. You can expire keys from the system.

Most clients follow the same rules. You can read the generic instructions and best practices on the memcached homepage.

If you really want to dig into it, I'd look at the source. Here's the header comment:

""" client module for memcached (memory cache daemon)  Overview ========  See U{the MemCached homepage<http://www.danga.com/memcached>} for more about memcached.  Usage summary =============  This should give you a feel for how this module operates::      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.  Detailed Documentation ======================  More detailed documentation is available in the L{Client} class. """ 
like image 134
Oli Avatar answered Sep 18 '22 16:09

Oli