Skip to main content

Fast and light-weight memcached client for C++/Python

Project description

build status pypiv pyversions wheel license

libmc is a memcached client library for Python without any other dependencies in runtime. It’s mainly written in C++ and Cython. libmc can be considered as a drop in replacement for libmemcached and python-libmemcached.

libmc is developing and maintaining by Douban Inc. Currently, It is working in production environment, powering all web traffics in douban.com. Realtime benchmark result is available on travis.

Build and Installation

For users:

pip install libmc

Usage:

import libmc

mc = libmc.Client(['localhost:11211', 'localhost:11212'])
mc.set('foo', 'bar')
assert mc.get('foo') == 'bar'

Under the hood

Under the hood, libmc consists of 2 parts: an internal fully-functional memcached client implementation in C++ and a Cython wrapper around that implementation. Dynamic memory allocation and memory-copy are slow, so we tried our best to avoid them. The set_multi command is not natively supported by the memcached protocol. Some techniques are applied to make set_multi command extremely fast in libmc (compared to some other similiar libraries).

Configuration

import libmc
from libmc import (
    MC_HASH_MD5, MC_POLL_TIMEOUT, MC_CONNECT_TIMEOUT, MC_RETRY_TIMEOUT
)

mc = libmc.Client(
    [
    'localhost:11211',
    'localhost:11212',
    'remote_host',
    'remote_host mc.mike',
    'remote_host:11213 mc.oscar'
    ],
    do_split=True,
    comp_threshold=0,
    noreply=False,
    prefix=None,
    hash_fn=MC_HASH_MD5,
    failover=False
)

mc.config(MC_POLL_TIMEOUT, 100)  # 100 ms
mc.config(MC_CONNECT_TIMEOUT, 300)  # 300 ms
mc.config(MC_RETRY_TIMEOUT, 5)  # 5 s
  • 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.

Contributing to libmc

Feel free to send a Pull Request. For feature requests or any questions, please open an Issue.

For SECURITY DISCLOSURE, please disclose the information responsibly by sending an email to security@douban.com directly instead of creating a GitHub issue.

FAQ

Does libmc support PHP?

No. But if you like, you can write a wrapper for PHP based on the C++ implementation.

Is Memcached binary protocol supported ?

No. Only Memcached ASCII protocol is supported currently.

Why reinventing the wheel?

Before libmc, we’re using python-libmemcached, which is a python extention for libmemcached. libmemcached is quite weird and buggy. After nearly one decade, there’re still some unsolved bugs.

Is libmc thread-safe ?

libmc is a single-threaded memcached client. If you initialize a libmc client in one thread but reuse that in another thread, a Python Exception ThreadUnsafe will raise in Python.

Is libmc compatible with gevent?

Yes, with the help of greenify, libmc is friendly to gevent. Read tests/shabby/gevent_issue.py for details.

Notice:

gevent.monkey.patch_all() will override threading.current_thread().ident to Greenlet’s ID, this will cause libmc to throw a ThreadUnSafe error or run into dead lock, you should only patch the things that you need, e.g.

from gevent import monkey
monkey.patch_socket()

Acknowledgments

Contributors

Who is using

Documentation

https://github.com/douban/libmc/wiki

LICENSE

Copyright (c) 2014-2020, Douban Inc. All rights reserved.

Licensed under a BSD license: https://github.com/douban/libmc/blob/master/LICENSE.txt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libmc-1.3.5.tar.gz (61.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

libmc-1.3.5-cp37-cp37m-macosx_10_15_x86_64.whl (152.0 kB view details)

Uploaded CPython 3.7mmacOS 10.15+ x86-64

File details

Details for the file libmc-1.3.5.tar.gz.

File metadata

  • Download URL: libmc-1.3.5.tar.gz
  • Upload date:
  • Size: 61.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/2.7.15

File hashes

Hashes for libmc-1.3.5.tar.gz
Algorithm Hash digest
SHA256 de41742c852c3522da03692b73e4f2647a68f2edf44acc8af89f911bef3b6ac3
MD5 fc4469e3ba2d916fa2cd9b1848beaf5b
BLAKE2b-256 863fd2c3689e5436cf23a2684ea21ee03de183b9ceeb44eb50f49428b233892e

See more details on using hashes here.

File details

Details for the file libmc-1.3.5-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: libmc-1.3.5-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 152.0 kB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4

File hashes

Hashes for libmc-1.3.5-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ae6f509d6619556d67e0a2af6b610f45bbda115f2087d73f28e0e695af8f495b
MD5 86a1c223e63cf6b8b9a41b3021f001eb
BLAKE2b-256 c54270cc2d8cb308093c7b6151ffa45ecc6d9fe84ec06a31b949a254f464dc73

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page