• Stars
    star
    442
  • Rank 98,677 (Top 2 %)
  • Language
    C++
  • License
    BSD 3-Clause "New...
  • Created over 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast and light-weight memcached client for C++ / #python / #golang #libmc

libmc

build_go build_py 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 [email protected] 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

More Repositories

1

DOUAudioStreamer

A Core Audio based streaming audio player for iOS and macOS
Objective-C
2,768
star
2

dpark

Python clone of Spark, a MapReduce alike framework in Python
Python
2,691
star
3

code

[DEPRECATED]Douban CODE
CSS
1,811
star
4

beansdb

Archived, see GoBeansDB instead.
C
870
star
5

douban-client

Python client library for Douban APIs (OAuth 2.0)
Python
744
star
6

rexxar-android

Mobile Hybrid Framework Rexxar Android Container
Java
667
star
7

rexxar-ios

Mobile Hybrid Framework Rexxar iOS Container
Objective-C
578
star
8

FRDIntent

A framework for handle the call between view controllers in iOS
Swift
492
star
9

gobeansdb

Distributed object storage server from Douban Inc.
Go
451
star
10

greenify

Make blocking C library work with gevent
C
427
star
11

ynm3k

UI Automation + YUItest driven acceptance tests that can be hooked into Jenkins
JavaScript
410
star
12

paracel

Distributed training framework with parameter server
C++
337
star
13

douban-objc-client

Objective-C client library for Douban APIs (OAuth 2.0)
Objective-C
254
star
14

beanseye

Proxy and monitor for beansdb in Go
Go
233
star
15

rexxar-web

Mobile Hybrid Framework Rexxar Web SDK
JavaScript
206
star
16

Kenshin

Kenshin: A time-series database alternative to Graphite Whisper with 40x improvement in IOPS
Python
206
star
17

tfmesos

Tensorflow in Docker on Mesos #tfmesos #tensorflow #mesos
Python
191
star
18

pymesos

A pure python implementation of Mesos scheduler and executor
Python
163
star
19

brownant

Brownant is a web data extracting framework.
Python
159
star
20

linguist

Language Savant, Python clone of github/linguist.
Python
153
star
21

graph-index

index of Graphite & Diamond
Python
129
star
22

CaoE

Kill all children processes when the parent dies
Python
101
star
23

douban-quixote

Douban's Quixote
Python
82
star
24

douban-utils

Douban's Utils
Python
59
star
25

python-libmemcached

DEPRECATED, use https://github.com/douban/libmc instead. python-libmemcached is a python extention for libmemcached
Python
57
star
26

PyCharlockHolmes

Character encoding detecting library for Python using ICU and libmagic.
Common Lisp
50
star
27

DOUSNSSharing

SNS OAuth 2 binding and sharing
Objective-C
47
star
28

ellen

Ellen is a wrapper of pygit2 and git command.
Python
41
star
29

Polymorph

Transform value of dictionary to property of Objective-C class, by using a `dynamic` like directive.
Objective-C
40
star
30

douban-sqlstore

Douban's MySQL lib.
Python
31
star
31

gpack

GIT Smart HTTP Server Rack Implementation, Python clone of https://github.com/schacon/grack
Python
30
star
32

douban-orz

The Missing Data Manager In Douban
Python
29
star
33

douban-mc

Douban's Memcached lib for python.
Python
27
star
34

charts

Helm charts from douban
Smarty
24
star
35

helpdesk

Yet another helpdesk based on multiple providers
Python
22
star
36

sina

A GIT Smart HTTP Server WSGI Implementation.
Python
21
star
37

sa-tools-core

Handy tools for sysadmin.
Python
18
star
38

graphite-kenshin

A plugin for using graphite-web with the kenshin-based storage backend.
Python
16
star
39

gobeansproxy

A proxy for GoBeansDB
Go
13
star
40

beansdbadmin

GoBeansDB Admin UI
Python
9
star
41

redarrow-rs

A command dispatcher to run executables remotely and safely.
Rust
6
star
42

MTURLProtocol

Multiple NSURLProtocol subclasses alternative solution.
Objective-C
4
star
43

python-libmagic

A wrapper for libmagic with static build.
Python
3
star
44

qiniu-exporter

Go
2
star
45

aliyun-exporter

Go
2
star
46

pyquicklz

C
1
star
47

upyun-exporter

Go
1
star
48

sa-tools-go

go version for sa-tools
Go
1
star