Skip to main content

小阳工具库Redis模块 — Redis便捷操作封装

Project description

xy-utilities-redis

PyPI version Python License: MIT

Redis Python 工具库,提供字符串、列表、集合、有序集合、哈希表等数据结构的简洁操作封装。

安装

pip install xy_utilities_redis

快速开始

from xy_utilities_redis import RedisHelper

# 推荐使用上下文管理器
with RedisHelper(host='127.0.0.1', port=6379) as redis:
    redis.string_set('key', 'value')
    print(redis.string_get('key'))  # 'value'

使用示例

String

redis.string_set('counter', 0)
redis.increase('counter')        # 1
redis.decrease('counter', 5)     # -4
value = redis.string_get('key')

List(队列)

redis.enqueue('queue', 'item1', 'item2')
item = redis.dequeue('queue')                    # 'item1'
items = redis.peek_range('queue', start=0, end=-1)  # 不出队,读取全部
length = redis.llen('queue')

Set

redis.set_add('myset', 'a', 'b', 'c')
redis.set_remove('myset', 'b')
members = redis.set_members('myset')  # ['a', 'c']

Sorted Set

redis.zset_add('ranking', {'Alice': 100, 'Bob': 90})
redis.zset_remove('ranking', 'Bob')
top = redis.zset_range('ranking', 0, -1)
by_score = redis.zset_range_byscore('ranking', min_score=80, max_score=100)
score = redis.zset_score('ranking', 'Alice')
redis.zset_remove_range('ranking', min_score=0, max_score=50)

Hash

redis.hash_set('user:1', {'name': 'Alice', 'age': '30'})
values = redis.hash_get('user:1', 'name', 'age')  # ['Alice', '30']
redis.hash_del('user:1', 'age')

Key 管理

keys = redis.key_get('user:*')
exists = redis.key_exists('user:1', 'user:2')  # 返回存在的 key 数量
redis.key_expire('session', 3600)              # 设置过期时间(秒)
ttl = redis.key_expiration('session')
redis.key_delete('session', 'tmp')

Pub/Sub

# 发布
redis.publish('channel', 'message')

# 订阅(handler 为回调函数)
pubsub = redis.subscribe('channel', lambda msg: print(msg['data']))
while True:
    pubsub.get_message()

API 参考

RedisHelper(**kwargs)

构造参数透传给 redis.StrictRedis

分类 方法 说明
String string_set(name, value) 设置字符串值
String string_get(name) → str 获取字符串值
String increase(name, amount=1) → int 自增
String decrease(name, amount=1) → int 自减
List enqueue(name, *values) → int 入队(RPUSH)
List dequeue(name, count) → str|list 出队(LPOP)
List peek_range(name, start, end) → list 读取不出队
List lindex(name, index) → str 按下标读取
List llen(name) → int 列表长度
Set set_add(name, *values) → int 添加成员
Set set_remove(name, *values) → int 移除成员
Set set_members(name) → list 获取所有成员
ZSet zset_add(name, mapping) → int 添加成员
ZSet zset_remove(name, *values) → int 移除成员
ZSet zset_range(name, start, end) → list 按排名范围获取
ZSet zset_range_byscore(name, min_score, max_score) → list 按分数范围获取
ZSet zset_score(name, member) → float 获取成员分数
ZSet zset_remove_range(name, min_score, max_score) → int 按分数范围删除
Hash hash_set(name, mapping) → int 批量设置字段
Hash hash_get(name, *keys) → list 获取字段值
Hash hash_del(name, *keys) → int 删除字段
Key key_get(pattern) → list 按 pattern 搜索 key
Key key_exists(*names) → int 检查 key 是否存在
Key key_type(name) → str 获取 key 的值类型
Key key_expire(name, time) → bool 设置过期时间
Key key_expiration(name) → int 获取剩余 TTL
Key key_delete(*names) → int 删除 key
Pub/Sub publish(channel, message) → int 发布消息
Pub/Sub subscribe(channel, handler) → PubSub 订阅频道

开发环境

conda create -n xy_utilities_redis python=3.11 -y
conda activate xy_utilities_redis
pip install build twine
pip install "redis>=6.2.0"

发布

rm -rf dist *.egg-info
python -m build

# 发布到私有 PyPI
twine upload -r packages-pypi dist/*

# 发布到 PyPI
python -m twine upload dist/*

License

MIT © Colin Chang

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

xy_utilities_redis-1.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

xy_utilities_redis-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file xy_utilities_redis-1.0.1.tar.gz.

File metadata

  • Download URL: xy_utilities_redis-1.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for xy_utilities_redis-1.0.1.tar.gz
Algorithm Hash digest
SHA256 08a05381cdfd70deb1a354ec4e1f6e74e36c3e497eeb96cc12a3994f2a79fe0d
MD5 629edbc192dfda0b3851c0ae5c431c03
BLAKE2b-256 d0199551e687474f8bc496cdb2edf7961b012eccb3b2c699d38ea6ef61181eb4

See more details on using hashes here.

File details

Details for the file xy_utilities_redis-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for xy_utilities_redis-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 14b07ed084368447a342be4ee2f305486d1398f0151dc137c7a1d07f08f4156b
MD5 92c572f25843fb60453669c860d3cb61
BLAKE2b-256 a4a0141afc338b731993eaa98c93714b22155dda41066a665412d97c776611a3

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