pyredishelper
提供redis客户端的代理对象功能.本项目代理的对象是redis-py中的四种客户端
redis.Redisredis.cluster.RedisClusterredis.asyncio.Redisredis.asyncio.cluster.RedisCluster
特性
- 提供了统一的代理对象
RedisProxy用于代理redis.Redis,redis.cluster.RedisCluster,redis.asyncio.Redis或redis.asyncio.cluster.RedisCluster - 针对生产者消费者模式提供了专用代理对象
ChannelConsumerHelper,ChannelProducerHelper,QueueConsumerHelper,QueueProducerHelper,StreamConsumerPHelper,StreamProducerHelper - 生产者消费者提供了进一步的封装,可以通过上下文管理连接
使用
RedisProxy的使用
我们可以使用RedisProxy延后初始化要代理的对象,从而避免客户端对象在各个函数之间传来传去.
rediscli = RedisProxy()
...
rediscli.initialize_from_url("redis://localhost:6379/0?decode_responses=true")
我们可以在运行时通过代理对象的propertyaio和propertycluster来确认是哪种客户端.
当我们编程时我们应该先确定将使用哪种客户端,然后使用typing.cast方法在调用前声明类型.多数时候我们并不需要标明实际的类型,可以只区分同步和异步和具体使用:
同步:
r = cast(redis.commands.core.DataAccessCommands,rediscli)
r.get("x")
异步:
r = cast(redis.commands.core.AsyncDataAccessCommands,rediscli)
await r.get(x)
这样我们在写程序是也可以有接口提示.只有用到pubsub,execute_command或者pipeline的少数场景时需要指定具体类型.
生产者消费者模式
生产者消费者模式提供了基本统一的接口设计
ConsumerProtocol通用的同步消费者接口ProducerProtocol除了stream外通用的同步生产者接口AioConsumerProtocol通用的异步消费者接口AioProducerProtocol除了stream外通用的异步生产者接口StreamProducerProtocolstream的同步生产者接口AioStreamProducerProtocolstream的异步生产者接口
当我们编程时我们应该先确定将使用哪种客户端,然后使用typing.cast方法在调用前声明对应代理的接口类型.
同步生产者
qp = cast(StreamProducerProtocol, StreamProducerHelper.from_proxy(rediscli, maxlen=20))
with qp.mount() as producer:
for i in range(10):
producer.publish(topic,value)
异步生产者
qp = cast(AioStreamProducerProtocol, StreamProducerHelper.from_proxy(rediscli, maxlen=20))
async with qp.mount() as producer:
for i in range(10):
await producer.publish(topic,value)
同步消费者
qc = cast(ConsumerProtocol, QueueConsumerHelper.from_proxy(rediscli, topics))
with qc.watch() as records:
for record in records:
print(f"get msg {record.value} from topic {record.topic}")
异步消费者
qc = cast(AioConsumerProtocol, QueueConsumerHelper.from_proxy(rediscli, topics))
async with qc.watch() as records:
async for record in records:
print(f"get msg {record.value} from topic {record.topic}")
安装
pip install pyredishelper
0.0.2
- 新增list,pubsub,stream3种常用生产者消费者结构的代理类型
- 细化注释文档
0.0.1
项目创建 MIT License
Copyright (c) 2023 Python-Tools
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Metadata
Release files for pyredishelper 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyredishelper-0.0.2.tar.gz | 12.4 kB | Without provenance | Details for pyredishelper-0.0.2.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyredishelper-0.0.2-py3.11.egg | Legacy Egg format | - | - | Details for pyredishelper-0.0.2-py3.11.egg |
| pyredishelper-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for pyredishelper-0.0.2-py3-none-any.whl |
Total release size: 47.3 kB
Release files / pyredishelper-0.0.2.tar.gz
| Download URL | pyredishelper-0.0.2.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2bfc79ab2284aa98e880249aa8b00f937eb3b0b0069c334679387d6deef66d36
|
|
BLAKE2b-256 checksum How to use checksums |
ae73d7d05472e591ea9a42776ed881c11b306606794f350314a9ac340eb5fee6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.2
|
Release files / pyredishelper-0.0.2-py3.11.egg
| Download URL | pyredishelper-0.0.2-py3.11.egg |
|---|---|
| Size | 16.8 kB |
| Tags | Egg |
|
SHA-256 checksum How to use checksums |
6114ba343f24a345ac929aaf036134d828628159cd9d9349ff9e433ae3e9213e
|
|
BLAKE2b-256 checksum How to use checksums |
a9c51b42474e508388cea0090a471c6e919bd24b7a6e73d7fe65ad6a62617565
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.2
|
Release files / pyredishelper-0.0.2-py3-none-any.whl
| Download URL | pyredishelper-0.0.2-py3-none-any.whl |
|---|---|
| Size | 18.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1dee53c3c883c7debe599f6fe04403e0dcb6041816301484578589d190a88f80
|
|
BLAKE2b-256 checksum How to use checksums |
fd705a2e3d129b1d07ff0214edf99267667f9f559316ddf789a9d3cb42b28665
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.2
|
Security for pyredishelper 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.