A pure python module (thread safe) to access memcached via it's binary with SASL auth support.
The main purpose of this module it to be able to communicate with memcached using binary protocol and support authentication, so it can work with Heroku for example.
Latest compiled docs on Read The Docs here.
Use pip or easy_install.
pip install python-binary-memcachedimport bmemcached
client = bmemcached.Client(('127.0.0.1:11211', ), 'user',
'password')
client.set('key', 'value')
print(client.get('key'))Using it with distributed keys
import bmemcached
client = bmemcached.DistributedClient(
('127.0.0.1:11211', ), 'user', 'password'
)
client.set('key', 'value')
print(client.get('key'))Using it with raw data
By default, the client keeps the type of each value in the memcached flags field.
Some applications already use the flags field for their own data, and a default
client then fails to read their keys. Set raw_data=True to read and write
those keys.
import bmemcached
client = bmemcached.Client(('127.0.0.1:11211', ), raw_data=True)
client.set('key', b'value')
print(client.get('key')) # b'value'In raw data mode:
- A read returns the stored
bytesvalue. The client ignores the flags field. - A write accepts only
bytesvalues. Other types raiseTypeError. - The client does not compress values. The default
compress_level=-1means no compression, and acompress_levelgreater than 0 raisesMemcachedException.
DistributedClient accepts the same raw_data argument.
Warning
A write in raw data mode sets the flags field of the key to 0. This overwrites the flags that another application set on the key.
python-binary-memcached unit tests are found in the test/ directory
and are designed to be run using pytest. pytest will discover the tests
automatically, so all you have to do is:
$ pytest
...
170 passed in 4.43 secondsThis runs the tests with the default Python interpreter.
You can also verify that the tests pass on other supported Python interpreters.
For this we use tox, which will automatically create a virtualenv for
each supported Python version and run the tests. For example:
$ tox
...
py310: commands succeeded
py311: commands succeeded
py312: commands succeeded
ERROR: py313: InterpreterNotFound: python3.13
py314: commands succeededYou may not have all the required Python versions installed, in which case you
will see one or more InterpreterNotFound errors.
If you want to use it with Django, go to django-bmemcached to get a Django backend.