Python SDK for mcache — high-performance in-memory cache with KV, Hash, List, Set data structures
Project description
mcache-py
mcache 的官方 Python 客户端 SDK —— 一个高性能内存缓存服务的 redis-py 风格客户端。
mcache 是用 Go 编写的高性能内存缓存服务,支持 KV / Hash / List / Set 四种数据结构,可嵌入或独立部署。 主仓库:github.com/atoncooper/mcache
特性
- 零依赖:纯 Python,仅使用标准库
- redis-py 风格 API:熟悉的命名 / 调用方式,迁移成本低
- 类型注解:完整 PEP 561 类型支持
- 连接池:内置线程安全的连接池
- 集群客户端:
ShardClient— 一致性哈希分片MasterSlaveClient— 主写从读SentinelClient— 自动故障切换
- 数据结构齐全:KV、Hash、List、Set 全部支持
安装
pip install mcache-py
需要 Python 3.10+。
快速开始
from mcache import Client
# 单机连接
with Client(host='127.0.0.1', port=11211) as c:
c.set('hello', b'world')
print(c.get('hello')) # b'world'
print(c.ping()) # True
Hash
c.hset('user:1', 'name', 'Alice')
c.hset('user:1', 'age', '30')
c.hgetall('user:1') # {'name': 'Alice', 'age': '30'}
c.hincrby('user:1', 'age', 1) # 31
List
c.lpush('queue', 'task-1', 'task-2')
c.rpush('queue', 'task-3')
c.lrange('queue', 0, -1) # ['task-2', 'task-1', 'task-3']
c.lpop('queue') # 'task-2'
Set
c.sadd('tags', 'go', 'python', 'rust')
c.sismember('tags', 'python') # True
c.smembers('tags') # ['go', 'python', 'rust']
c.scard('tags') # 3
TTL / Key 管理
c.set('session', b'abc123', ttl=300) # 300 秒后过期
c.ttl('session') # 剩余秒数
c.expire('session', 600) # 重置 TTL
c.persist('session') # 移除 TTL
c.exists('session') # True
c.type('session') # 'string'
集群模式
ShardClient — 一致性哈希分片
from mcache import ShardClient
sc = ShardClient([
('10.0.0.1', 11211, 2), # (host, port, weight)
('10.0.0.2', 11211, 1),
('10.0.0.3', 11211, 1),
])
sc.set('hello', b'world') # 自动路由到对应节点
sc.close()
MasterSlaveClient — 读写分离
from mcache import MasterSlaveClient
ms = MasterSlaveClient(
master=('10.0.0.1', 11211),
slaves=[('10.0.0.2', 11211), ('10.0.0.3', 11211)],
)
ms.set('k', b'v') # 写 → master
ms.get('k') # 读 → slave (round-robin)
SentinelClient — 自动故障切换
from mcache import SentinelClient
sc = SentinelClient(
master=('10.0.0.1', 11211),
replicas=[('10.0.0.2', 11211), ('10.0.0.3', 11211)],
)
sc.set('k', b'v') # 自动跟随当前 master
sc.master_addr # 查看当前 master
错误处理
from mcache import Client, KeyNotFoundError, ServerError, ConnectionError
try:
val = c.get('missing-key') # 返回 None,不抛异常
except KeyNotFoundError as e:
pass
except ServerError as e:
print('server error:', e)
except ConnectionError as e:
print('connection failed:', e)
完整异常类型:
| 异常 | 说明 |
|---|---|
McacheError |
所有异常的基类 |
ConnectionError |
连接失败 / 中断 |
ReadTimeout |
读取超时 |
KeyNotFoundError |
键不存在 |
ServerError |
服务端返回错误 |
ProtocolError |
协议异常 / 响应损坏 |
InvalidCommandError |
无效命令 |
PoolExhaustedError |
连接池耗尽 |
API 参考
Client(host='127.0.0.1', port=11211, pool_size=4, timeout=10.0)
| 方法 | 说明 |
|---|---|
get(key) |
取值,不存在返回 None |
set(key, value, ttl=0) |
写值,ttl 单位秒 |
delete(key) |
删除键 |
len() |
总条目数 |
cleanup() |
清理过期条目 |
ping() |
连通性检测 |
exists(key), type(key) |
键检查 |
expire(key, sec), pexpire(key, ms) |
设置 TTL |
ttl(key), pttl(key) |
查询 TTL |
persist(key) |
移除 TTL |
keys(pattern='*') |
模式匹配 |
完整命令列表参见 文档。
启动 mcache 服务
# 下载 mcache server(Go 二进制)
git clone https://github.com/atoncooper/mcache.git
cd mcache
go build -o bin/mcache ./cmd/mcache
./bin/mcache server --config config.yaml
# 默认监听 127.0.0.1:11211
或用 Docker:
docker run -d -p 11211:11211 atoncooper/mcache:latest
性能
在本地回环、64 客户端并发、200K 操作下:
| 指标 | mcache | Redis |
|---|---|---|
| 吞吐 (ops/s) | ~XXX K | ~XXX K |
| p50 延迟 | XX us | XX us |
| p99 延迟 | XXX us | XXX us |
详见 性能对比测试。
链接
- 主仓库:https://github.com/atoncooper/mcache
- 完整文档:https://github.com/atoncooper/mcache/tree/master/docs
- Issue 反馈:https://github.com/atoncooper/mcache/issues
- 变更日志:CHANGELOG.md
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcache_py-1.0.0.tar.gz.
File metadata
- Download URL: mcache_py-1.0.0.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70f037ba773aa71c24116903fddb549f6b46730cf1e27ac843ab834305c00261
|
|
| MD5 |
e6c2aa52f440aac8d6bbe76cb52c3669
|
|
| BLAKE2b-256 |
c64e4d59cb2714b1dcaf251e3ae2502221fefbfd2acdb76c76facac32bc4458e
|
Provenance
The following attestation bundles were made for mcache_py-1.0.0.tar.gz:
Publisher:
python-publish.yml on atoncooper/mcache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcache_py-1.0.0.tar.gz -
Subject digest:
70f037ba773aa71c24116903fddb549f6b46730cf1e27ac843ab834305c00261 - Sigstore transparency entry: 1516502722
- Sigstore integration time:
-
Permalink:
atoncooper/mcache@ad441204becf893efd8815b4c885c498c3b01413 -
Branch / Tag:
refs/tags/py-v1.0.0 - Owner: https://github.com/atoncooper
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ad441204becf893efd8815b4c885c498c3b01413 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mcache_py-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mcache_py-1.0.0-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccee2fb7413cebc620978dafd60d1f02718dda03599eeb96517f453ffcce8e8d
|
|
| MD5 |
9687fbb25fafd63ed424dc0d89d3eaa9
|
|
| BLAKE2b-256 |
9aa55da41ceb0a8134d3cd61147db7fc12107b36d891a82f69bd91ce695e02b1
|
Provenance
The following attestation bundles were made for mcache_py-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on atoncooper/mcache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcache_py-1.0.0-py3-none-any.whl -
Subject digest:
ccee2fb7413cebc620978dafd60d1f02718dda03599eeb96517f453ffcce8e8d - Sigstore transparency entry: 1516502774
- Sigstore integration time:
-
Permalink:
atoncooper/mcache@ad441204becf893efd8815b4c885c498c3b01413 -
Branch / Tag:
refs/tags/py-v1.0.0 - Owner: https://github.com/atoncooper
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ad441204becf893efd8815b4c885c498c3b01413 -
Trigger Event:
push
-
Statement type: