CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pylibmc

Quick and small memcached client for Python

86

1.03x
Overview
Eval results
Files

configuration.mddocs/

Configuration

Comprehensive behavior management for performance tuning and feature configuration. pylibmc provides extensive configuration options through behaviors, hash algorithms, distribution methods, and connection parameters.

Capabilities

Behavior Management

Configure client behavior for optimal performance and functionality. Behaviors control everything from network timeouts to hashing algorithms.

def get_behaviors() -> dict:
    """
    Get current behavior settings with symbolic names.
    
    Returns:
    dict: Current behaviors with string keys and appropriate values
    """

def set_behaviors(behaviors: dict):
    """
    Set behavior configuration options.
    
    Parameters:
    - behaviors (dict): Behavior name to value mapping
    
    Raises:
    - ValueError: For unknown behavior names or invalid values
    """

@property
def behaviors() -> BehaviorDict:
    """
    Behavior dictionary property that syncs changes to client.
    Allows both getting and setting behaviors via dict interface.
    
    Returns:
    BehaviorDict: Special dict that updates client on changes
    """

Hash Algorithms

Configure hashing algorithms for key distribution across multiple servers.

# Hash algorithm constants
hashers: dict = {
    "default": hash_default,
    "md5": hash_md5,
    "crc": hash_crc,
    "fnv1_64": hash_fnv1_64,
    "fnv1a_64": hash_fnv1a_64,
    "fnv1_32": hash_fnv1_32,
    "fnv1a_32": hash_fnv1a_32,
    "murmur": hash_murmur,
    "hsieh": hash_hsieh,
    "jenkins": hash_jenkins
}

hashers_rvs: dict  # Reverse mapping: constant to name

Distribution Algorithms

Configure distribution algorithms for spreading keys across server clusters.

# Distribution algorithm constants
distributions: dict = {
    "modula": distribution_modula,
    "consistent": distribution_consistent,
    "random": distribution_random,
    "consistent ketama": distribution_consistent_ketama,
    "consistent ketama spy": distribution_consistent_ketama_spy,
    "consistent weighted": distribution_consistent_weighted,
    "virtual bucket": distribution_virtual_bucket,
    "consistent max": distribution_consistent_max
}

distributions_rvs: dict  # Reverse mapping: constant to name

Available Behaviors

Complete list of configurable behaviors and callbacks.

all_behaviors: list = [
    "auto_eject",
    "buffer_requests", 
    "cas",
    "connect_timeout",
    "distribution",
    "failure_limit",
    "hash",
    "ketama",
    "ketama_hash",
    "ketama_weighted",
    "no_block",
    "num_replicas",
    "pickle_protocol",
    "receive_timeout",
    "retry_timeout", 
    "send_timeout",
    "tcp_keepalive",
    "tcp_nodelay",
    "verify_keys"
]

all_callbacks: list = [
    "namespace"
]

BehaviorDict Class

Special dictionary class that automatically syncs behavior changes to the client.

class BehaviorDict(dict):
    """
    Dictionary that syncs behavior changes to client automatically.
    Inherits from dict with additional client synchronization.
    """
    
    def __init__(client, *args, **kwargs):
        """
        Initialize behavior dictionary.
        
        Parameters:
        - client: Client instance to sync changes to
        - args, kwargs: Standard dict initialization arguments
        """

    def __setitem__(name: str, value):
        """
        Set behavior and sync to client.
        
        Parameters:
        - name (str): Behavior name
        - value: Behavior value
        """

    def update(d: dict):
        """
        Update multiple behaviors and sync to client.
        
        Parameters:
        - d (dict): Behaviors to update
        """

Configuration Utilities

Helper functions for configuration management.

def translate_server_spec(server: str, port: int = 11211, weight: int = 1) -> tuple:
    """
    Parse server specification string into connection tuple.
    
    Parameters:
    - server (str): Server specification (host:port, /path/to/socket, etc.)
    - port (int): Default port if not specified
    - weight (int): Server weight for consistent hashing
    
    Returns:
    tuple: (server_type, address, port, weight)
    """

def translate_server_specs(servers: list) -> list:
    """
    Parse list of server specifications.
    
    Parameters:
    - servers (list): List of server specifications
    
    Returns:
    list: List of parsed server tuples
    """

Configuration Categories

Network and Connection Behaviors

# Connection timeouts (milliseconds)
client.behaviors.update({
    "connect_timeout": 5000,      # Connection establishment timeout
    "send_timeout": 5000,         # Send operation timeout  
    "receive_timeout": 5000,      # Receive operation timeout
    "retry_timeout": 30,          # Retry failed servers after N seconds
})

# Network optimization
client.behaviors.update({
    "tcp_nodelay": True,          # Disable Nagle's algorithm
    "tcp_keepalive": True,        # Enable TCP keepalive
    "no_block": True,             # Non-blocking I/O operations
    "buffer_requests": True,      # Buffer multiple requests
})

Hashing and Distribution

# Hash algorithm selection
client.behaviors.update({
    "hash": "fnv1a_64",          # Use FNV1a 64-bit hash
    "ketama_hash": "md5",        # Hash for ketama distribution
})

# Distribution algorithm
client.behaviors.update({
    "distribution": "consistent ketama",  # Consistent hashing
    "ketama": True,                       # Enable ketama consistent hashing
    "ketama_weighted": True,              # Weight servers by memory
    "num_replicas": 160,                  # Virtual nodes per server
})

Reliability and Failover

# Server failure handling  
client.behaviors.update({
    "auto_eject": True,           # Remove failed servers automatically
    "failure_limit": 3,           # Max failures before server ejection
})

Protocol and Serialization

# Protocol options
client.behaviors.update({
    "cas": True,                  # Enable Compare-And-Store support
    "verify_keys": True,          # Validate key format
})

# Serialization
client.behaviors.update({
    "pickle_protocol": 2,         # Python pickle protocol version
})

Usage Examples

Basic Configuration

import pylibmc

# Create client with behaviors
client = pylibmc.Client(["localhost:11211"], binary=True)

# Set behaviors for optimal performance
client.behaviors = {
    "tcp_nodelay": True,
    "ketama": True,
    "no_block": True,
    "connect_timeout": 5000,
    "retry_timeout": 30
}

# Check current configuration
print("Current behaviors:", client.behaviors)

High-Performance Configuration

import pylibmc

client = pylibmc.Client(["server1:11211", "server2:11211"], binary=True)

# Optimize for high-throughput scenarios
high_perf_behaviors = {
    # Network optimization
    "tcp_nodelay": True,
    "no_block": True,
    "buffer_requests": True,
    
    # Consistent hashing for even distribution
    "distribution": "consistent ketama",
    "ketama": True,
    "hash": "fnv1a_64",
    "ketama_hash": "md5",
    
    # Fast failover
    "auto_eject": True,
    "failure_limit": 2,
    "retry_timeout": 10,
    
    # Reasonable timeouts
    "connect_timeout": 2000,
    "send_timeout": 2000,
    "receive_timeout": 2000,
}

client.behaviors.update(high_perf_behaviors)

Multi-Server Cluster Configuration

import pylibmc

# Define server cluster with weights
servers = [
    ("server1.cache.local", 11211, 1),  # Standard weight
    ("server2.cache.local", 11211, 2),  # Double weight (more memory)
    ("server3.cache.local", 11211, 1),  
]

client = pylibmc.Client(servers, binary=True)

# Configure for cluster reliability
cluster_behaviors = {
    # Consistent hashing with weights
    "distribution": "consistent ketama",
    "ketama": True,
    "ketama_weighted": True,
    "num_replicas": 160,
    
    # Robust failure handling
    "auto_eject": True,
    "failure_limit": 3,
    "retry_timeout": 60,
    
    # Conservative timeouts for stability
    "connect_timeout": 5000,
    "send_timeout": 10000,
    "receive_timeout": 10000,
}

client.behaviors.update(cluster_behaviors)

Development vs Production Configurations

import pylibmc
import os

def create_client(servers, environment="development"):
    """Create client with environment-specific configuration."""
    
    client = pylibmc.Client(servers, binary=True)
    
    if environment == "development":
        # Development: Faster timeouts, more verbose errors
        client.behaviors.update({
            "connect_timeout": 1000,
            "send_timeout": 1000, 
            "receive_timeout": 1000,
            "retry_timeout": 5,
            "verify_keys": True,
            "tcp_nodelay": True,
        })
    else:
        # Production: Conservative timeouts, high performance
        client.behaviors.update({
            "connect_timeout": 5000,
            "send_timeout": 5000,
            "receive_timeout": 5000,
            "retry_timeout": 30,
            "tcp_nodelay": True,
            "no_block": True,
            "buffer_requests": True,
            "auto_eject": True,
            "failure_limit": 3,
            "ketama": True,
            "distribution": "consistent ketama",
        })
    
    return client

# Usage
dev_client = create_client(["localhost:11211"], "development")
prod_client = create_client(["cache1:11211", "cache2:11211"], "production")

Behavior Validation

import pylibmc

client = pylibmc.Client(["localhost:11211"])

# Validate behavior settings
def validate_behaviors(client):
    """Check if client has recommended behaviors."""
    behaviors = client.get_behaviors()
    
    recommendations = {
        "tcp_nodelay": True,
        "ketama": True,
        "connect_timeout": lambda x: 1000 <= x <= 10000,
        "retry_timeout": lambda x: x >= 10,
    }
    
    for behavior, expected in recommendations.items():
        current = behaviors.get(behavior)
        
        if callable(expected):
            valid = expected(current) if current is not None else False
        else:
            valid = current == expected
            
        status = "✓" if valid else "✗"
        print(f"{status} {behavior}: {current}")

validate_behaviors(client)

Server Type Constants

server_type_tcp: int    # TCP connection (value: 1)
server_type_udp: int    # UDP connection (value: 2)  
server_type_unix: int   # Unix socket connection (value: 4)

Feature Detection

support_compression: bool   # Whether zlib compression is available
support_sasl: bool         # Whether SASL authentication is available
libmemcached_version: str  # Version of underlying libmemcached

Install with Tessl CLI

npx tessl i tessl/pypi-pylibmc

docs

client-operations.md

configuration.md

connection-pooling.md

error-handling.md

index.md

tile.json