Python Binary Memcached Client

0.31.4 · active · verified Fri Apr 17

A pure Python module for accessing Memcached servers using the binary protocol, including support for SASL authentication and TLS. The current version is 0.31.4, with releases occurring periodically to add features and improvements.

Common errors

Warnings

Install

Imports

Quickstart

Initializes a `bmemcached.Client` instance, sets a string value, retrieves it, and then deletes it. Ensure a Memcached server is running at `127.0.0.1:11211`.

import bmemcached
import os

# Configure client with Memcached server address, optional username, and password
# Use environment variables for sensitive information
servers = ['127.0.0.1:11211']
username = os.environ.get('MEMCACHED_USER', '')
password = os.environ.get('MEMCACHED_PASSWORD', '')

mc = bmemcached.Client(servers, username, password)

# Set a key-value pair
mc.set('my_key', 'Hello, Memcached!')
print(f"Set 'my_key': {mc.get('my_key')}")

# Get a key-value pair
value = mc.get('my_key')
if value:
    print(f"Retrieved 'my_key': {value.decode('utf-8')}")
else:
    print("'my_key' not found")

# Delete a key
mc.delete('my_key')
print(f"Deleted 'my_key'. Now 'my_key' is: {mc.get('my_key')}")

view raw JSON →