-
Notifications
You must be signed in to change notification settings - Fork 75
Configuration
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-
servers: is a list of memcached server addresses. Each address can be in format ofhostname[:port] [alias].portandaliasare optional. Ifportis not given, default port11211will be used.aliaswill be used to compute server hash if given, otherwise server hash will be computed based onhostandport(i.e.: Ifportis not given or it is equal to11211,hostwill be used to compute server hash. Ifportis not equal to11211,host:portwill be used). -
do_split: Memcached server will refuse to store value if size >= 1MB, ifdo_splitis 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. Ifbuffer length > comp_threshold > 0, it will be compressed using zlib. Ifcomp_threshold = 0, string buffer will never be compressed using zlib. default:0 -
noreply: Whether to enable memcached'snoreplybehaviour. default:False -
prefix: The key prefix. default:'' -
hash_fn: hashing function for keys. possible values:MC_HASH_MD5MC_HASH_FNV1_32MC_HASH_FNV1A_32MC_HASH_CRC_32
default:
MC_HASH_MD5NOTE: 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_TIMEOUTTimeout parameter used during set/get procedure. (default:300ms) -
MC_CONNECT_TIMEOUTTimeout parameter used when connecting to memcached server on initial phase. (default:100ms) -
MC_RETRY_TIMEOUTWhen a server is not available dur to server-end error. libmc will try to establish the broken connection in everyMC_RETRY_TIMEOUTs until the connection is back to live.(default:5s)
NOTE: The hashing algorithm for host mapping on continuum is always md5.