Skip to content
PAN, Myautsai edited this page Feb 4, 2016 · 1 revision

A sample configuration:

import libmc


def libmc_from_config(config):
    '''
    Sample config:
    {
        'SERVERS': ['localhost:11211 mc-server111',
                    'localhost:11212 mc-server112'],
        'HASH_FUNCTION': 'crc32',
        'PREFIX': '',
        'CONNECT_TIMEOUT': 10,
        'POLL_TIMEOUT': 300,
        'RETRY_TIMEOUT': 5,
        'COMPRESSION_THRESHOLD': 1024
    }
    '''

    _HASH_FN_MAPPING = {
        'md5': libmc.MC_HASH_MD5,
        'crc32': libmc.MC_HASH_CRC_32,
        'fnv1_32': libmc.MC_HASH_FNV1_32,
        'fnv1a_32': libmc.MC_HASH_FNV1A_32
    }
    servers = config['SERVERS']
    hash_fn = _HASH_FN_MAPPING[config.get('HASH_FUNCTION', 'md5')]
    prefix = config.get('PREFIX', '')
    connect_timeout = config.get('CONNECT_TIMEOUT', 10)
    poll_timeout = config.get('POLL_TIMEOUT', 300)
    retry_timeout = config.get('RETRY_TIMEOUT', 5)
    comp_threshold = config.get('COMPRESSION_THRESHOLD', 0)
    client = libmc.Client(
        servers,
        comp_threshold=comp_threshold,
        prefix=prefix,
        hash_fn=hash_fn,
    )
    client.config(libmc.MC_CONNECT_TIMEOUT, connect_timeout)
    client.config(libmc.MC_POLL_TIMEOUT, poll_timeout)
    client.config(libmc.MC_RETRY_TIMEOUT, retry_timeout)
    return client

Parameters

  • servers: is a list of memcached server addresses. Each address can be in format of hostname[:port] [alias]. port and alias are optional. If port is not given, default port 11211 will be used. alias will be used to compute server hash if given, otherwise server hash will be computed based on host and port (i.e.: If port is not given or it is equal to 11211, host will be used to compute server hash. If port is not equal to 11211, host:port will be used).

  • do_split: Memcached server will refuse to store value if size >= 1MB, if do_split is enabled, large value (< 10 MB) will be splitted into several blocks. If the value is too large (>= 10 MB), it will not be stored. default: True

  • comp_threshold: All kinds of values will be encoded into string buffer. If buffer length > comp_threshold > 0, it will be compressed using zlib. If comp_threshold = 0, string buffer will never be compressed using zlib. default: 0

  • noreply: Whether to enable memcached's noreply behaviour. default: False

  • prefix: The key prefix. default: ''

  • hash_fn: hashing function for keys. possible values:

    • MC_HASH_MD5
    • MC_HASH_FNV1_32
    • MC_HASH_FNV1A_32
    • MC_HASH_CRC_32

    default: MC_HASH_MD5

    NOTE: fnv1_32, fnv1a_32, crc_32 implementations in libmc are per each spec, but they're not compatible with corresponding implementions in libmemcached.

  • failover: Whether to failover to next server when current server is not available. default: False

  • MC_POLL_TIMEOUT Timeout parameter used during set/get procedure. (default: 300 ms)

  • MC_CONNECT_TIMEOUT Timeout parameter used when connecting to memcached server on initial phase. (default: 100 ms)

  • MC_RETRY_TIMEOUT When a server is not available dur to server-end error. libmc will try to establish the broken connection in every MC_RETRY_TIMEOUT s until the connection is back to live.(default: 5 s)

NOTE: The hashing algorithm for host mapping on continuum is always md5.

ALSO SEE