Skip to main content

No project description provided

Project description

pyredishelper

提供redis客户端的代理对象功能.本项目代理的对象是redis-py中的四种客户端

  • redis.Redis
  • redis.cluster.RedisCluster
  • redis.asyncio.Redis
  • redis.asyncio.cluster.RedisCluster

特性

  • 提供了统一的代理对象RedisProxy用于代理redis.Redis,redis.cluster.RedisCluster,redis.asyncio.Redisredis.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的同步生产者接口
  • AioStreamProducerProtocol stream的异步生产者接口

当我们编程时我们应该先确定将使用哪种客户端,然后使用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.

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

pyredishelper-0.0.2.tar.gz (12.4 kB view details)

Uploaded Source

Built Distributions

pyredishelper-0.0.2-py3.11.egg (16.8 kB view details)

Uploaded Source

pyredishelper-0.0.2-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file pyredishelper-0.0.2.tar.gz.

File metadata

  • Download URL: pyredishelper-0.0.2.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyredishelper-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2bfc79ab2284aa98e880249aa8b00f937eb3b0b0069c334679387d6deef66d36
MD5 9124bbfadca50af82ba3f04696586864
BLAKE2b-256 ae73d7d05472e591ea9a42776ed881c11b306606794f350314a9ac340eb5fee6

See more details on using hashes here.

File details

Details for the file pyredishelper-0.0.2-py3.11.egg.

File metadata

  • Download URL: pyredishelper-0.0.2-py3.11.egg
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyredishelper-0.0.2-py3.11.egg
Algorithm Hash digest
SHA256 6114ba343f24a345ac929aaf036134d828628159cd9d9349ff9e433ae3e9213e
MD5 fc2dbb17c33d4d90f5bce9c595298741
BLAKE2b-256 a9c51b42474e508388cea0090a471c6e919bd24b7a6e73d7fe65ad6a62617565

See more details on using hashes here.

File details

Details for the file pyredishelper-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pyredishelper-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1dee53c3c883c7debe599f6fe04403e0dcb6041816301484578589d190a88f80
MD5 75c87500fbe84a00ffe06a9611f2d36d
BLAKE2b-256 fd705a2e3d129b1d07ff0214edf99267667f9f559316ddf789a9d3cb42b28665

See more details on using hashes here.

Supported by

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