This article was reproduced from: http://my.oschina.net/flynewton/blog/10660
Copy the MEMCACHED.PYC to the working directory
1 #!/usr/bin/env python2 3 Importmemcache4 5MC = Memcache. Client (['127.0.0.1:12000'],debug=0)6Mc.set ("Foo","Bar")7Value = Mc.get ("Foo")8 PrintValue
Output to get Bar
4.python-memcached API Summary
The main methods are as follows:
1 @set (key,val,time=0,min_compress_len=0)
The setting of the unconditional key-value pair, where time is used to set the timeout, in seconds, and Min_compress_len to set zlib compression (note: Zlib is a library that provides data compression)
@set_multi (mapping,time=0,key_prefix= ", min_compress_len=0)
Set multiple key-value pairs,key_prefix is the key prefix, the full key name is Key_prefix+key, use the following method
1>>> Mc.set_multi ({'K1': 1,'K2': 2}, key_prefix='pfx_') == []2 3>>> Mc.get_multi (['K1','K2','nonexist'], key_prefix='pfx_') == {'K1': 1,'K2': 2}
@add (key,val,time=0,min_compress_len=0)
Add a key-value pair, internally calling the _set () method
@replace (key,val,time=0,min_compress_len=0)
Replace value, internal call _set () method
@get (Key)
Get value according to key, error return None
@get_multi (keys,key_prefix= ")
Gets the value of multiple keys and returns a dictionary. List of keys as key
@delete (key,time=0)
Delete a key. Time is in seconds and is used to ensure that the set and update operations fail at a specific period. If the return is not 0 then the success
@incr (key,delta=1)
Increment variable plus delta, default plus 1, use the following
1 >>> mc.set ("counter") 23 >>> mc.incr ("counter")4 5 67 @decr (key,delta=1)
Decrement variable minus delta, minus 1 by default
5._set method
Many methods internally call the _set method , the source code is as follows:
Note: memcached clients use TCP links to communicate with the server, a running Memcached server monitors some ports, the client connects these ports, sends commands to the server, reads the response, and finally closes the connection. (Please refer to the Chinese version of the Memcached agreement for specific commands.)
6.python-memcached Thread Safety
Python-memcached is not thread safe
The answer is yes, why we need thread-safe memcached client, because our actual application is generally multithreaded model, such as CherryPy, twisted, if python-memcached is not thread-safe, The problem is not only the concurrent modification of shared variables is so simple, it is the data flow of external socket link is chaotic
How does python-memcached implement thread-safe? View Source code See
1 Try:2 #Only exists in Python 2.4+3 fromThreadingImportLocal4 exceptImporterror:5 #Todo:add The Pure-python local implementation6 classlocal (object):7 Pass8 9 classClient (local):
It is trickery to let the client class inherit threading.local, that is, every property in the client is bound to the current thread. Although not very elegant, but very real. But don't think you can use python-memcached in your thread, because this thread local practice, your application must use the thread pool mode, but not constantly create destroy thread, because each new thread is created, For the use of a new client, that is, a new socket link, if you open the creation of the destruction thread, it will cause the creation of the destruction of the socket link, resulting in a lot of performance degradation. Fortunately, both cherrypy and twisted are using the thread pool pattern .
How to use python-memcached package